PowerShell 7 Tutorials for Beginners #10 : Error Handling (Try Catch Finally)

preview_player
Показать описание
In this video I will go over how to handle errors in PowerShell 7.2 , We will see how to use the try catch statement and the finally statement as well. We will also be seeing how to set the default error action and the error history.

Tags:
PowerShell, jackedprogrammer, powershell 7, how to install powershell 7, powershell 7 on vs code, visual studio code
Рекомендации по теме
Комментарии
Автор

Jack I am quite surprised that you are not getting more hits as these videos are really good. Thanks

richardgarrow
Автор

Thank you so much for creating this series. As a beginner, the series helped me to become fearless about powershell.

shashikantovhal
Автор

The quality of these videos man! I know for sure you'll make it big here

aeylia
Автор

Nice Job! bite sized chunks, easy to digest. I will be subscribing

jadelise
Автор

This series was very helpful .. thank you so much!

kennethpaige
Автор

This is amazing! Thank you for uploading

fonsohalbert
Автор

One of my favorite things to do with error handling in PowerShell is to structure the catch statement like this:
$ErrorMessage = "Error occurred when attempting to do X on Y. This is a terminating error.`n$($Error[0])"
Write-ErrorLog -LogMessage = $ErrorMessage
return $ErrorMessage

This will spit out a custom error message that you write yourself, and then on a new line spit out the text from the last error that happened (which is the one you just caught).
It makes debugging easier.

MrWogle
Автор

$ErrorActionPreference = "Stop"
try {
$filePath = "C:\Scripts\10 - Error Handling"
$files = Get-ChildItem -Path $filePath

foreach ($file in $files) {
Write-Output $file.Name
}

Write-Output "This is after the loop"
} catch {
Write-Output "Caught an error"
} finally {
Write-Output "This always runs no matter what!"
}

netor-y