Are there any official guidelines from Microsoft about when to add -Confirm
, -Force
, and -WhatIf
parameters to custom PowerShell cmdlets?
I haven't researched whether the documentation is this detailed, but the following are based on my observations:
You should use -WhatIf
for anything that makes a change. Updates are changes that can benefit from -WhatIf
(e.g., what if you want to make a lot of updates?).
-Force
means "force overwrite of an existing item" or "override a read-only file system attribute". In either case the success of the action depends on the user having permission.
-Confirm
and -Force
are not mutually exclusive. For example, you can confirm an action to write a file, but the file might be protected with the read-only attribute. In this case the action would fail unless you also specify -Force
.