
Imagine you are feeling handy and start out to rebuild your bathroom. Now imagine you’re standing knee deep in water in your basement. I bet you wished that somewhere in your head there was a tiny voice which had asked you, “do you really want to drill there”? This is the purpose of the PowerShell -Confirm parameter. If you’re running a cmdlet which changes the system state you can use -Confirm in order to get a question asking you if you really want to go ahead with the change.
Argh, my files, my precious files!
We’ve all heard horror stories of format c: and the like. At one point you might even have deleted files by mistake. With the Remove-Item cmdlet you can do just that.
PS C:\Temp> Remove-Item * <- That went smooth!
PS C:\Temp>
However wouldn’t this have been better:
PS C:\Temp> Remove-Item * -Confirm
Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile1.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile2.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\junkfile3.txt".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
Confirm
Are you sure you want to perform this action?
Performing operation "Remove File" on Target "C:\Temp\single-copy-of-expense-report.xlsx".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): n
Lessons Learned
As the carpenters say; measure twice, cut once. Sometimes it’s better to step carefully unless you are really sure of what you are doing. The -Confirm parameter can be that little voice inside your head.
