Thursday, March 19, 2020

Why won't try/catch work and catch my powershell error

So this drove me nuts for a long time and I couldn't find anything on the net to figure out why Powershell didn't function like the c# code I've written, especially considering Powershell is based and written in c#.

 The below will not fall into the catch portion of the code, why is that?
 

try
{
       Remove-DistributionGroupMember -Identity "#Test" -Member "blah@blah.com";
       
}
catch
{
       $ErrorMessage = $_.Exception.Message; Write-host "This is Exception.Message $ErrorMessage"
       $FailedItem = $_.Exception.ItemName; Write-host "This is Exception.ItemName $FailedItem"
}

 
We have to set $ErrorActionPreference = "Stop";

 If you run it after doing that, you'll fall into the catch portion of the code like you want to.