Wednesday, October 7, 2015

Correct way to handle exception for Get-SPWeb and Get-SpSite SharePoint PowerShell

Catch block does not work correctly for exceptions from get-spweb and get-spsite without the
-ErrorAction Stop parameter.

 This code will not print "in catch block"

 try{Get-SPWeb 'wrong url'} catch { Write-Host "in catch block" }

 correct usage : try{Get-SPWeb 'wrong url' -ErrorAction Stop } catch { Write-Host "in catch block" }