How To Handle On Error silentlyContinue or ErrorAction -Windows PowerShell

1 answer

Staff July 10, 2016

If some portion of powershell scripts throws error, one best solution is to tell it to continue silently on error.
For example:

$SrvName = “Ccleaner”
$Service = Get-Service -display $SrvName
Start-service $srvname

I want to start Ccleaner service but it does’t exists in this case it wll throw error that Ccleaner service doesn’t exists I can skip that by adding, -ErrorAction SilentlyContinue – just after

$Service = Get-Service -display $SrvName -ErrorAction SilentlyContinue 

Now if I add at start I can skip any error that script has.

$ErrorActionPreference = “silentlycontinue”

For example I use Powershell script to Map a network drive, but once drives are connected running the same script may result in multiple error so I add $ErrorActionPreference = “silentlycontinue” at the start.

$ErrorActionPreference = “silentlycontinue”
$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive(“r:”, “\\g-10\learn” )
$net.MapNetworkDrive(“k:”, “\\g-10\PICTURES”)


Please login or Register to Submit Answer