Saturday, May 19, 2012

Automating the deletion of orphaned users' network folders with PowerShell

We have a process that deletes the network folders of a user as they leave the university, whether it be a student graduation/transferring or an employee leaving/being terminated. Generally speaking, this process is pretty well tuned, but occasionally hiccups do happen and sometimes folders are left behind for users that don't exist anymore. I wrote a little PowerShell script to handle that. It needs to be run as a user that has access to remove these folders as well as one that can read AD user objects (any domain user by default).




My users' home folders are laid out in \\server\share\first_letter\username 
My folder would be at \\folder\share\m\marra for example. If yours is different, you'll have to tweak the line that starts with $directories. It relies on the folders having the exact same name as the user's username, which is really quite common. It also makes use of -exclude, because there are a few directories on the top level that are for shared folders that I needed excluded for obvious reasons. If your share only contains the user's folders, then you can drop the -exclude switch.


Also, as with any script that uses rm, del, or remove-item, COMMENT IT OUT FIRST! Run it once with the output going to the console only. Dry runs are always important to do when you're running any script that you find on the Internet!

Thursday, May 10, 2012

Deleting very long file paths on NTFS volumes

It's the end of the Spring semester here, and that means that it's time to clean up the year's mess. Typically, this means disabling AD accounts for graduates, removing their network shares, etc. We have a monolithic home-grown VB program for this (gross, I know), but occasionally some oddities slip though the cracks.

One such oddity was the network folder of one of the computer science students. The path on the server looked like this:

E:\users\u\username\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\csc290\assignment8\

except that it went on FOREVER. There we no files in place, there were no hard links or junctions in play. There were just a ton of folders of the same name nested inside of each other. I, literally, have no idea how the student was able to do this, but it happened.

I tried navigating deep using \\?\E:\users\u\username\ blah blah blah, but even using that syntax, I couldn't delete it. After about an hour of trying to use subst (over and over), to shorten the path and trying to find a command-line delete tool that works with long file paths, I finally decided to give robocopy a try.

I ended up using the /mir switch to mirror an empty folder into the screwed up one. The command looked like this:

robocopy c:\empty e:\users\u\username\csc290 /mir

This took a little while to run, but eventually the top csc290 directory was emptied and I was able to delete the user's folder without a problem.