8.3. Using the whatif Parameter

I think that the whatif parameter is an incredibly valuable part of the Windows PowerShell approach. It allows you to test the effect of a command before anything is changed on a machine. This is very useful if, as shown in earlier examples, you use the remove-item cmdlet with a wildcard.

Another situation where the whatif parameter is useful is with the stop-process and stop-service cmdlets.

8.3.1. Using the stop-process Cmdlet

You can use the stop-process cmdlet to stop one or more running processes on a machine. But for obvious reasons, this can be dangerous, and Windows PowerShell builds in a couple of safety mechanisms. First, is the -whatif parameter I describe below. But the -whatif parameter isn't the only safety mechanism built into the stop-process cmdlet. Imagine that you type a command like the following:

stop-process

If you were familiar with the behavior of the get-process Cmdlet, you might expect the command to accept a default to stop all running processes - since this would not be a very safe approach, fortunately this is not what happens. In this case, stop-process prompts you for a process ID for a process to stop, as shown in Figure 8-10.

Figure 8.10. Figure 8-10

The stop-process cmdlet has -ID, -processname, -input, and -passthru parameters. The -ID parameter is the only positional parameter and is recognized in position 1. ...

Get Professional Windows® PowerShell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.