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.