Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
July 13, 2023

To Err is Human, To Understand is the PowerShell Pro

I don’t think I’ve discussed how PowerShell handles errors in any detail, and you can’t be an effective toolmaker or PowerShell user unless you understand how PowerShell handles situations when things go wrong. I’m sure you’ve all seen situations like this.

PS C:\> Get-Service Bits,Foo,WinRm
Get-Service: Cannot find any service with service name 'Foo'.

Status   Name               DisplayName
------   ----               -----------
Stopped  Bits               Background Intelligent Transfer Servi…
Running  WinRm              Windows Remote Management (WS-Managem…

This is how PowerShell 7 handles exceptions. I’ll eventually dive into changes in PowerShell 7. This is what I expect many of you are used to.

PS C:\> Get-Service Bits,Foo,WinRm
Get-Service : Cannot find any service with service name 'Foo'.
At line:1 char:1
+ Get-Service Bits,Foo,WinRm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Foo:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceComm 
   and


Status   Name               DisplayName
------   ----               -----------
Stopped  Bits               Background Intelligent Transfer Ser...
Running  WinRm              Windows Remote Management (WS-Manag...
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.