If (Test-Path 'Cert:\LocalMachine') { Set-Location Cert:\LocalMachine } Else { Write-Host 'Path not detected!' $LASTEXITCODE=0 #EXIT 0 } Try { Get-ChildItem -Recurse | Where-Object {$_.Subject -eq "CN=COMMLINK, DC=DOMAIN, DC=COM"} | Remove-Item Write-Host "Certificate removed successfully!" $LASTEXITCODE=0 #EXIT 0 } Catch { Write-Host "Certificate cannot be removed!" $LASTEXITCODE=0 #EXIT 0 }
Notes
Suppressing errors in your PowerShell script
$ErrorActionPreference= ‘silentlycontinue’