dany2010 Posted July 15, 2013 Posted July 15, 2013 Hi, We have lots of folders in our home directory directory that are no longer linked to existing accounts. I've tried to find a script but they all seem to be for linux. Is there a way/script to delete these. The home folder names are identical to their usernames. Thanks
Gatt Posted July 15, 2013 Posted July 15, 2013 If your Home Folders are based on usernames, then a PowerShell script to read in all the folders to a CSV files, and then run the CSV against all usernames in AD, and delete the folder if $samaccountname doesn't exist You'd need to play with the powershell code to compare the folder name and samaccountname... 1
linkazoid Posted July 25, 2014 Posted July 25, 2014 @dany2010 did you ever solve this. I'm in a similar position now and looking for the same solution.
dany2010 Posted July 29, 2014 Author Posted July 29, 2014 Hi Linkazoid Did it manually in the end. Had a afternoon free so sat down with a Mc Donalds and clicked away comparing AD with the folders.
RobOgan Posted July 29, 2014 Posted July 29, 2014 Spiceworks lists a Powershell script for this here: Clean Home Folders Not Matching Active Directory Accounts - Script Center - Spiceworks 1
linkazoid Posted July 30, 2014 Posted July 30, 2014 Thank you. Not quite sure why I couldn't find it on the internet. Looks to be exactly what we need!
Sephiroth Posted July 30, 2014 Posted July 30, 2014 I know you've already solved this but if anyone is interested, this is the method I would use to do this. It assumes that you have all of your Home Directories in one place (but can be modified with just a couple of extra lines to search within a directory tree) and that your directory names are the same as your Usernames: $Homes = Get-ChildItem "" Foreach ($HomeDir in $Homes) { $Name = $HomeDir.Name If (DSQuery User -SAMID $Name) { Write-Host "Account Exists for $Name" } Else { Remove-Item $HomeDir -Recurse Write-Host "Deleted directory: $Name" } }
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now