Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering Windows PowerShell Scripting
  • Table Of Contents Toc
Mastering Windows PowerShell Scripting

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
4.4 (16)
close
close
Mastering Windows PowerShell Scripting

Mastering Windows PowerShell Scripting

4.4 (16)
By: Brenton J.W. Blawat

Overview of this book

If you are a system administrator who wants to become an expert in controlling and automating your Windows environment, then this book is for you. Prior knowledge of PowerShell's core elements and applications is required for this book.
Table of Contents (16 chapters)
close
close
15
Index

Error and exception handling – Try/Catch


One of the more popular error and exception handling techniques is leveraging Try/Catch methodology. The Try/Catch block is used for handling terminating errors and has a very simple structure. You first use the Try { } section of code and then use Catch { } to catch any errors and perform actions based on the errors. In the instance that you catch an error, you can access the exception object by declaring $_. The $_ refers to what is in the current pipeline. Since an error occurred during the Try sequence, the data in the pipeline is the actual error information.

To use the Try/Catch block, do the following action:

Try {
    1+ "abcd"
}
Catch {
  Write-host "Error Processing the Command: $_"
}
Write-host "" 
Write-host "Attempting to Add a String without Exception Handling:"
1+ "abcd"

The output of this is shown in the following screenshot:

The preceding example shows how you have the ability to leverage the Try/Catch block during runtime. In this example...

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Mastering Windows PowerShell Scripting
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon