marsh-wiggle
marsh-wiggle

Reputation: 2803

powershell: How to show only the message when throw exception?

I need to throw an exception when a directory is not found and suppress the detail informations.

if( ! (Test-Path -Path $destDir)  )
{
    throw "Directory $destDir not found!" 
}

In this case the message is shown and the details, where it was thrown in the code.

Is there a way to show only the message without the details?

(I tested throw ... 2>$null but that doesn't work)

Upvotes: 1

Views: 4618

Answers (1)

David Brabant
David Brabant

Reputation: 43499

Use $_.Exception.Message in your catch block. More information here.

Upvotes: 2

Related Questions