# Clearing files from directories

My code does what I want, which is remove any files from these specific folders. Is there a smoother way to do this?

# Test Script to remove any items

Remove-Item -Path 'C:\users\test_removal_account\desktop\*.*' -Recurse
Remove-Item -Path 'C:\users\test_removal_account\contacts\*.*' -Recurse
Remove-Item -Path 'C:\users\test_removal_account\documents\*.*' -Recurse
Remove-Item -Path 'C:\users\test_removal_account\Music\*.*' -Recurse
Remove-Item -Path 'C:\users\test_removal_account\Pictures\*.*' -Recurse
Remove-Item -Path 'C:\users\test_removal_account\Videos\*.*' -Recurse

• PowerShell supports for loops. – πάντα ῥεῖ Apr 23 '18 at 17:02
• Your code assumes the user hasn't relocated the standard folders and is a bit redundant. Also files without extension will be left. – LotPings Apr 23 '18 at 17:03

There's an important principal in programming called Don't Repeat Yourself (or DRY). This says that if there is any repeated code, then that is usually a bad thing. So let's identify the non-repeated code, and start with that. This is the stuff that changes:

desktop
contacts
documents
Music
Pictures
Videos


We could put that in a file, but that's probably overkill here, so let's just put that in an array:

$dirNames = "desktop", "contacts", "documents", "Downloads", "Music", "Pictures", "Videos"  Now we can just operate on the array: foreach ($dirName in $dirNames) { Remove-Item -Path "C:\users\test_removal_account\$dirName\*" -Recurse -WhatIf
}


Notice I put -WhatIf on the Remove-Item. That just makes the command tell you what it is going to do instead of actually doing it. This is useful for testing purposes. You can remove it when you are happy the script does what you want.

This script as it stands is dangerous. Recursive deletes must be treated with respect. If you accidentally blow away someone's work, you could ruin their life. You may even accidentally blow away all your own work and ruin your own life. You have to think about this sort of thing as a responsible engineer.

Of course, the "test_removal_account" makes the script pretty benign as it stands, but I don't know what you are going to replace that with in the real script.

I don't think a script ought do anything destructive if you just run it.

If it were me, I would put in a confirmation check before deleting. I would get the user to type in something, such as "really delete" before doing the delete.

• "I don't think a script ought do anything destructive if you just run it." Amen to that! – Josiah Apr 26 '18 at 6:44
• Thanks for providing a useful and helpful understanding of what right looks like. The purpose of the script is to intentionally delete a users content on shutdown. These computers are in are in a lab environment and the users are not supposed to be saving anything long term. (There are other ways to do this (i.e. Guest account on Windows 10) which I am doing now... but Guest account also prevents Windows Update... soooo this script should resolve 2 issues). – xerxes2985 Apr 26 '18 at 19:25
• @xerxes2985, okay, cool. You should probably add -Force to the Remove-Item because otherwise if a student makes a file read-only, I think it won't be deleted. – Dangph Apr 27 '18 at 1:58