Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly
Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly
Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
4. Examine the output from the Get-Process cmdlet and identify the process ID. The output<br />
on my machine is shown here. Please note that in all likelihood, the process ID used <strong>by</strong> your<br />
instance of Notepad.exe will be different from the one on my machine.<br />
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName<br />
------- ------ ----- ----- ----- ------ -- -----------<br />
39 2 944 400 29 0.05 1056 notepad<br />
5. Use -whatif to see what would happen if you used Stop-Process to stop the process ID you<br />
obtained in step 4. This process ID will be found under the Id column in your output. Use the<br />
-id parameter to identify the Notepad.exe process. The <strong>com</strong>mand is as follows:<br />
Stop-Process -id 1056 -whatif<br />
6. Examine the output from the <strong>com</strong>mand. It tells you that the <strong>com</strong>mand will stop the Notepad<br />
process with the process ID that you used in your <strong>com</strong>mand.<br />
What if: Performing operation "Stop-Process" on Target "notepad (1056)"<br />
Confirming actions<br />
As described in the previous section, you can use -whatif to prototype a cmdlet in <strong>Windows</strong> <strong>PowerShell</strong>.<br />
This is useful for seeing what a cmdlet would do; however, if you want to be prompted before the<br />
execution of the cmdlet, you can use the -confirm argument. The cmdlets used in the "Confirming the<br />
execution of cmdlets" procedure are listed in the ConfirmingExecutionOfCmdlets.txt file.<br />
Confirming the execution of cmdlets<br />
1. Open <strong>Windows</strong> <strong>PowerShell</strong>, start an instance of Notepad.exe, identify the process, and examine<br />
the output, just as in steps 1 through 4 in the previous exercise.<br />
2. Use the -confirm argument to force a prompt when using the Stop-Process cmdlet to stop the<br />
Notepad process identified <strong>by</strong> the Get-Process note* <strong>com</strong>mand. This is shown here:<br />
Stop-Process -id 1768 -confirm<br />
The Stop-Process cmdlet, when used with the -confirm argument, displays the following confirmation<br />
prompt:<br />
Confirm<br />
Are you sure you want to perform this action?<br />
Performing operation "Stop-Process" on Target "notepad (1768)".<br />
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help<br />
(default is "Y"):<br />
3. Type y and press Enter. The Notepad.exe process ends. The <strong>Windows</strong> <strong>PowerShell</strong> prompt<br />
returns to the default, ready for new <strong>com</strong>mands, as shown here:<br />
PS C:\><br />
8 <strong>Windows</strong> <strong>PowerShell</strong> 3 <strong>Step</strong> <strong>by</strong> <strong>Step</strong>