An exception is a runtime problem that can interrupt execution and be handled deliberately. PowerShell distinguishes exceptions from nonterminating errors, which commonly display an error and continue processing other items. section 24.1
Handle exceptions when a script must respond to an expected failure or preserve evidence for later diagnosis:
- Use terminating behavior when a command failure must enter error handling; configure it with -ErrorAction Stop or, when necessary, temporarily set $ErrorActionPreference to Stop. section 24.4 section 24.5
- Wrap risky work in Try and respond in Catch. Use ErrorVariable when processing can continue and failures should be collected rather than stopping every operation. section 24.4
- Use multiple typed Catch blocks for different failure types and Finally for cleanup or work that must always run; prefer Try/Catch over deprecated Trap. section 24.6
Handling an unreachable remote computer
A remote session may fail because a computer is offline, nonexistent, or incompatible with the selected protocol. Run New-PSSession with -ErrorAction Stop inside Try, then Catch can warn, log the computer name, or retry with another protocol. For several computers, choose whether one failure should stop all connections or be recorded while the remaining sessions continue. section 24.4
Get-Service -Name Foo,BITS,Nobody,WinRM -ErrorAction Continue Get-Service -Name BITS,Nobody,WinRM -ErrorAction SilentlyContinue Get-Service -Name BITS,Nobody,WinRM -ErrorAction Inquire Get-Service -Name BITS,Nobody,WinRM -ErrorAction Ignore Get-Service -Name BITS,Nobody,WinRM -ErrorAction Stop
Compare Continue, SilentlyContinue, Inquire, Ignore, and Stop while running Get-Service against a missing service. section 24.1
























