Sam
Sam

Reputation: 11

Powershell Catch Exception

I'm trying this to catch an exception but it just doesn't work rather just shows me error in the script editor I'm running the script from:-

Path I mentioned in the script "\server\abc" doesn't really exist, so it should catch it as an exception which it is not. HELP HELP

Try
{

Get-ChildItem -Path "\\server\abc"

}
Catch
{

 Write-Host "error"

}

Upvotes: 1

Views: 1737

Answers (1)

JNK
JNK

Reputation: 65147

You need to set the erroraction to STOP for the error to be terminating - only terminating errors are raised to a catch block.

Try
{

Get-ChildItem -Path "\\server\abc" -ErrorAction Stop

}
Catch
{

 Write-Host "error"

}

Upvotes: 8

Related Questions