Jump to content

Recommended Posts

Posted

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

Posted

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...

  • Thanks 1
  • 1 year later...
Posted

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.

Posted

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"
   }
}

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...