Patrick Ogenstad bio photo

Patrick Ogenstad

This site is in hybernation mode. To view my current work please visit Networklore.

Twitter LinkedIn Github

What if you just deleted 5000 accounts from your active directory domain by accident, what if there had been an easy way to make sure you were doing the right thing before the deletion. Would you have wanted to use it? I know I would. Just imagine you were planning on deleting all accounts which had been disabled for six months but you forgot some important switch and most of your accounts were gone because of that stupid switch. Ok, so this might be a bit far fetched but these things can happen and if you are not 100 percent sure of what you are doing, it’s great to be able to see what the results of your actions will be before you take them.

Let’s say we’ve been taking random notes and just want to kill all notepad processes without having to choose if we want to save every single file. Working with the Get-Process, Where-Object and Stop-Process cmdlets we decide to kill all the processes which has “note” in them.

PS C:\> Get-Process | Where-Object {$_.ProcessName -match "note"} | Stop-Process -Whatif
What if: Performing operation "Stop-Process" on Target "notepad (4284)".
What if: Performing operation "Stop-Process" on Target "notepad (4524)".
What if: Performing operation "Stop-Process" on Target "notepad (4848)".
What if: Performing operation "Stop-Process" on Target "notepad++ (4684)".
What if: Performing operation "Stop-Process" on Target "ONENOTE (5944)".
What if: Performing operation "Stop-Process" on Target "ONENOTEM (3876)".
PS C:\>

This would have wiped out every instance of notepad.exe along with my Onenote and notepad++ processes which was not what I wanted.

Lame example?

Yes, I guess you’re right that was quite a lame example. However I think it does illustrate how you can use the -whatif parameter. It can be very useful if you are working with important data and don’t want to make mistakes. Also it’s useful if your just playing around with PowerShell and learning what all the fuss is about.