I was working a script and because of a small typo very nearly lost most of the data on my C: drive. It’s very easy to get wrong, so treat this as a cautionary tale. The example below shows the error I made and the potential impact. I’ve used the command line instead of my script in the example, but the effect is the same.
First of all I set a variable to point to a folder off the root of the C: drive. Then I cleared the contents of the folder using the variable together with the Remove-Item command.
The screenshot below shows how to do this correctly.
Now look what happens when you mistype the name of the variable. PowerShell basically ignores the variable and assumes you want to remove everything from the root of the drive.
In my case only the fact that I didn’t use the -recurse parameter saved me. If I had used the -recurse parameter I would have lost everything except for items protected by the system.