8.4. Using the confirm Parameter

The -confirm parameter allows you to step through the processing of a cmdlet and decide at each point whether to allow it to implement the intended action or to prevent it taking that action.

When you use the confirm parameter, you are offered the options of Yes(Y), YestoAll(A), No(N), Notoall(L), Suspend(S), and Help(?).

This example repeats the preceding example but uses the Confirm parameter in place of the whatif parameter to demonstrate the difference in behavior. Notice that with the -confirm parameter you never get an overview of what you are going to do. You need to evaluate each action individually. However, when using the -whatif parameter, if you find that you can see the actions you want, then changing to the -confirm parameter allows you to step through the available actions, confirming those that you want to take place and rejecting those that you don't want.

Type the following command:

stop-service -servicename *sql* -confirm

Figure 8-16 shows the result after responding No to the first option offered and before making a decision about the second option.

Figure 8.16. Figure 8-16

The first part of the statement:

stop-service -servicename *sql*

would stop several SQL Server services if the -confirm parameter weren't present. For each service, you are asked to specify whether you want to stop that service, not stop that service, stop ...

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.