# Tahir Hassan's Blog

My Technical Notes

## Thursday, 9 February 2017

### PowerShell: Checking if -WhatIf switch is specified

You must add the attribute CmdletBinding with SupportsShouldProcess set to true onto your function if you want to take advantage of the -WhatIf switch.

In order to check if the -WhatIf switch is specified, use the $WhatIfPreference variable:  Function Test-Function { [CmdletBinding(SupportsShouldProcess=$true)]
param()

[bool]$WhatIfPreference.IsPresent }  Calling Test-Function -WhatIf will return true and without will return false. WhatIf is propagated to functions called within the function, therefore, in the following code, Outer called with -WhatIf still returns true and false without. Therefore we do not have to propagate it ourselves.  Function Outer { [CmdletBinding(SupportsShouldProcess=$true)]
param()

Test-Function
}