PowerShell - Catching Error not exporting to Out-File
<p>I have a script to list permissions on a remote machine.  The try{} works perfectly without errors, which is fine.  The catch{} however does not.  The script block is below:</p>

<pre><code>Try{
    $group = get-wmiobject win32_group -ComputerName $CompName -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
    $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
    $list = Get-WmiObject win32_groupuser -ComputerName $CompName -Filter $query
    $list | %{$_.PartComponent} | % {$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}-ErrorAction Stop| Out-File C:\AdminList\$CompName\AdminListResults\$CompName.txt
    Write-Host "Sequence Completed. Logs have been exported to $CompName folder." -ForegroundColor Cyan
}

catch{
    $Error[0]| Out-File C:\AdminList\$CompName\ErrorLogs\$CompName.txt
    Write-Host "Errors Found. See ErrorLogs for details" -ForegroundColor Yellow
}
</code></pre>

<p>What I was hoping to have it do was that if the try{} failed, it catches the error displayed, hides it from the console and exports it to a location specified but this is the part not working.  What is currently happening is that the try{} runs, the error is displayed in the console and the write-host from the try{} appears, NOT the catch{} write host.</p>

<p>Any advice would be great. I browsed Stack for answers but couldn't find any. My last question was closed because it was too broad so I hope this has more detail for better understanding.</p>

<p>Thank You!</p>