Anyone who has once run a delete command with a wildcard in the wrong place knows the wish: First show me what you would do. PowerShell has a built-in answer for it that sits in every well-built cmdlet: -WhatIf. This lesson shows how it works, how to build it into your own functions and where it ends. It is the most important lesson of the course, because everything that follows (runbooks, tests, automation) stands on it.

All examples were run with PowerShell 7.6.9 on Windows 11; the output comes from that run. PowerShell's messages are English (system language); "What if" is the output of -WhatIf.

Why a preview is best practice

A change has three phases: you decide what should change; the script works out what has to be done for that; then it does it. Errors almost always arise in the second phase (wrong filter, wrong path, wrong name), and they show up only in the third, when it is too late. -WhatIf puts a rehearsal between the second and the third: the script works it out and says what it would do, without doing it. You read the list, and only then do you let the real run follow.

This is best practice, but with a limitation this course names again and again: -WhatIf shows what your script believes it will do. It does not prove that the change succeeds (the target server can say "no"), not that it is wise, and not that all changes of your script stand behind the preview. Whether the last point holds is your job as the author; more on that in a moment.