Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Tuesday, August 18, 2009

PowerShell: What if I do this???



In PowerShell we have the ability to see what a command will do before it executes. Image when you were first starting out in IT. Someone told you to type Format C: without telling you the results. Obviously formatting the C: drive is not something you want to do on accident. You can ask PowerShell what would happen if you did _______. We use the -WhatIf parameter.

Let's first get a list of all services running on you client:
Get-Process


You noticed that a process is called BlueToothHeadsetProxy is listed. What would happen if you typed:
Stop-Process -ID 5200
(5200 is the process ID )

You would disconnect your headset. We can use the -Whatif parameter to discover what would happen if we would execute this command without executing it. To do this type:
Stop-Process -ID 5200 -whatif

stop-process -id 5200 -whatif
What if: Performing operation "Stop-Process" on Target "BluetoothHeadsetProxy (5200)".

We can also confirm each step before it is taken:
Stop-Process -ID 5200 -confirm

Confirm
Are you sure you want to perform this action?
Performing operation "Stop-Process" on Target "BluetoothHeadsetProxy (5200)".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help
(Default is "Y")

The Suspend option has its value when you are typing a long command, but then remember that something else needs to be done first. By adding the -Confirm parameter, you can suspend the execute. This gives you a chance to complete the missed step and then to use the up arrows to retrieve the long command and execute it without having to retype it.



No comments: