PowerShell for Windows Admins

Aug 12 2014   10:29AM GMT

Can it -whatif

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One of the nice things about PowerShell is that it can help you prevent mistakes. Many of the cmdlets that make changes to you system have a –whatif parameter that allows you to test your actions:

 

£> Get-Process | Stop-Process -WhatIf
What if: Performing the operation “Stop-Process” on target “armsvc (1564)”.
What if: Performing the operation “Stop-Process” on target “audiodg (3004)”.
What if: Performing the operation “Stop-Process” on target “concentr (7080)”.
What if: Performing the operation “Stop-Process” on target “conhost (3628)”.

etc

 

The –whatif parameter is only present on cmdlets that make changes and then only if the team writing the cmdlet implemented it – they should but you can’t guarantee it happened. So how can you find out which cmdlets implement –whatif?

Use Get-Command

Compare these 2 commands.

£> Get-Command -Module CimCmdlets | select Name

Name
—-
Export-BinaryMiLog
Get-CimAssociatedInstance
Get-CimClass
Get-CimInstance
Get-CimSession
Import-BinaryMiLog
Invoke-CimMethod
New-CimInstance
New-CimSession
New-CimSessionOption
Register-CimIndicationEvent
Remove-CimInstance
Remove-CimSession
Set-CimInstance

 

shows the cmdlets in a module

 

£> Get-Command -Module CimCmdlets -ParameterName Whatif | select Name

Name
—-
Invoke-CimMethod
New-CimInstance
Remove-CimInstance
Remove-CimSession
Set-CimInstance

 

Now you can test a module to see which cmdlets have –whatif enabled.  You can also test at just the cmdlet level:

£> Get-Command *process -ParameterName Whatif  -CommandType cmdlet | select Name

Name
—-
Debug-Process
Stop-Process
£> Get-Command *wmi* -ParameterName Whatif  -CommandType cmdlet | select Name

Name
—-
Invoke-WmiMethod
Remove-WmiObject
Set-WmiInstance

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: