PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login
No Result
View All Result
PowershellGuru
No Result
View All Result
Home Powershell Fundamentals

Understanding Error handling in Powershell [2021]

themeanmachine19@gmail.com by [email protected]
August 20, 2021
in Powershell Fundamentals
0
error handling in powershell
Share on FacebookShare on Twitter

You might also like

Powershell filter

Powershell filter: Using where-object and select-object (2021)

September 9, 2021
Function in PowerShell

A step-by-step guide for function in Powershell in 2021

September 1, 2021

When it comes to writing code, error management is a given. Expected behavior can frequently be checked and validated. We use exception handling when anything unexpected happens. You can easily handle exceptions thrown by other people’s code, or you can create your own for others to handle.

Error handling in PowerShell

What is Exception in PowerShell ?

An exception is a type of event that is thrown when standard error handling fails to resolve the problem. Attempting to divide a number by zero or running out of memory are both examples of exceptions. When specific problems arise, the creator of the code you’re using may create exceptions.

Throw, Try and Catch in PowerShell

When an exception occurs, we refer to it as a thrown exception. You must catch a thrown exception in order to manage it. The script will cease operating if an exception is thrown that isn’t captured by something.

Similarly, we have Try where we can put any logic and use try to catch the exception. Below are some examples of throw, try and catch respectively.

We throw an exception with the throw keyword to generate our own exception event.

				
					function hi
 {
  throw "hi all"
 }
 
				
			

This throws a runtime exception, which is a fatal error. A catch in a calling function handles it, or the script leaves with a notice like this.

				
					 hi

hi all
At line:3 char:1
+ throw "hi all"
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (hi all:String) [], RuntimeException
    + FullyQualifiedErrorId : hi all
				
			

In PowerShell (and many other languages), exception handling works by first trying a portion of code and then catching it if it throws an error. Here’s a sampling of what I’m talking about.

				
					function hi
{
throw "hi all"

 }
 try hi
{
   
catch
{
    Write-Output "Something threw an exception"
}

##Output:

Something threw an exception
				
			

Finally block in PowerShell

You don’t always need to handle an error, but you do need some code to run whether or not an exception occurs. This is exactly what a finally block does. The code inside finally will eventually get executed.

				
					function hi
{
throw "hi all"

 }

 try
{
    hi
}

catch
{
    Write-Output "Something threw an exception"
}

Finally

{
"PowershellGuru"
}

## Output ##

Something threw an exception
PowershellGuru
				
			

Cmdlet -ErrorAction

When you use the -ErrorAction Stop option with any advanced function or cmdlet, it converts all Write-Error statements into terminating errors that stop execution or can be caught.

Similarly, we have -ErrorAction SilentlyContinue which doesn’t display the error if any is there and continues without any break.

Let me show you how I use these in a script.

				
					$ErrorActionPreference = "Stop"

script-start
'
'
'
script-end


				
			

Instead of “Stop” we can use “SilentlyContinue”. The perks of using like this are we don’t have to mention it on every line where it is required.

Conclusion

In this post we have covered what is exception, try/catch/throw/finally with examples. Also, we have seen -erroraction cmdlets and how to actually use them. It must have been known now how the exception handling in Powershell is important. Let’s meet in our next post.

close

DON’T MISS A POST

Keep up to date with PowershellGuru

Powershell Blogs

PowershellGuru provides the best PowerShell scripts available that can be used and download freely. Do Check our blogs to get updated regularly.

Check your inbox or spam folder to confirm your subscription.

themeanmachine19@gmail.com

[email protected]

Related Stories

Powershell filter

Powershell filter: Using where-object and select-object (2021)

by [email protected]
September 9, 2021
0

Step-by-step guide for function in PowerShell. Function in Powershell. Function with the return value. Function scope. Function examples.

Function in PowerShell

A step-by-step guide for function in Powershell in 2021

by [email protected]
September 1, 2021
0

Step-by-step guide for function in PowerShell. Function in Powershell. Function with the return value. Function scope. Function examples.

Useful Powershell commands

10 Useful Powershell commands that everyone should know

by [email protected]
August 10, 2021
0

Powershell cmdlets. Powershell cmdlets list. Powershell cmdlets example. Get-process. Get-command. Set-executionpolicy. Clear-history. Stop-process.

Pipeline in Powershell

3 Proven ways to Learn Pipeline in Powershell

by [email protected]
July 4, 2021
0

Pipeline in Powershell. Powershell Pipeline. select-object. sort-object. measure-object. Learn Pipeline in Powershell. PowershellGuru.

Next Post
Function in PowerShell

A step-by-step guide for function in Powershell in 2021

Please login to join discussion

Recommended

Mirroring two AD groups using PowerShell

Mirroring two AD groups using PowerShell (Fast)

December 14, 2021
Mistakes To Avoid While Writing A Powershell Script

5 Big Mistakes To Avoid While Writing A Powershell Script

April 30, 2022

About

Dhrub Bharali

PowerShell Enthusiast

Dhrub is hardcore Powershell enthusiast, he has wriiten more than 100 powershell scripts and he is the sole owner of PowerShellGuru.

Follow Us

Popular Story

  • Installing software remotely using powershell

    Easy way to install software remotely using PowerShell (2021)

    827 shares
    Share 331 Tweet 207
  • Detect Log4j vulnerable servers using PowerShell

    753 shares
    Share 301 Tweet 188
  • How to find NTP Server using PowerShell?

    738 shares
    Share 295 Tweet 185
  • Get-LocalGroupMember: Find Local admin using PowerShell (2021)

    728 shares
    Share 291 Tweet 182
  • Get installed software list quickly using PowerShell (2021)

    695 shares
    Share 278 Tweet 174
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

No Result
View All Result
  • Home
  • Active Directory Scripts
  • Script Repository
  • DHCP Scripts
  • DNS Scripts
  • Blogs
  • Community
  • Login

© 2022 PowershellGuru- PowerShell Scripts For Automation

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.