windows

Powershell - Handling Errors

Error Handling in Powershell Try, Catch, Finally Try: Contains the code that might throw an error. Catch: Contains the code to handle the error if one occurs. Finally: Contains code that runs regardless of whether an error occurred or not, often used for cleanup tasks. try { # Code that might throw an error $result = Get-Content -Path "C:\nonexistentfile.txt" } catch { # Code to handle the error Write-Host "An error occurred: $_" } finally { # Code that runs regardless of an error Write-Host "This runs no matter what.

Powershell Notes

Powershell Notes PS C:\Users\aleescob> $PSVersionTable Name Value ---- ----- PSVersion 5.1.22621.2506 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.22621.2506 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 PS C:\Users\aleescob> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 22621 2506 Filter Based on the Verb and Noun PS C:\Users\aleescob> get-command -Verb delete CommandType Name Version Source ----------- ---- ------- ------ Function Delete-DeliveryOptimizationCache 1.0.3.0 DeliveryOptimization PS C:\Users\aleescob> get-command -Verb update CommandType Name Version Source ----------- ---- ------- ------ Function Update-AutologgerConfig 1.