PowerShell - Setting $ErrorActionPreference for the entire script PowerShell - Setting $ErrorActionPreference for the entire script powershell powershell

PowerShell - Setting $ErrorActionPreference for the entire script


Since you're running V3, you also have the option of using $PSDefaultParameterValues:

$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

Normally that will change it in the global scope. If you want to isolate it to just the local or script scope, you can initialize a new one in the local scope first:

$PSDefaultParameterValues = @{}$PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

If you want to inherit what's already in the parent scope and then add to it for the local scope:

 $PSDefaultParameterValues = $PSDefaultParameterValues.clone() $PSDefaultParameterValues += @{'New-RegKey:ErrorAction' = 'Stop'}

To set the default ErrorAction for all cmdlets, not just New-RegKey, specify '*:ErrorAction' instead of 'New-RegKey:ErrorAction' in the code above.