Jump to content

Home Folder Deletion - Finding 'Orphaned' Home Folders and Deleting Them


Recommended Posts

Posted

I've got a list from Active Directory of all the home folders in use (i.e. \\servername\staff$\homefoldername) so if I get a directory listing from the server is it possible to do some 'magic' and compare the two things to see which home folders I can safely delete as they are not linked to a current AD account?

 

Or is there another/easier/simpler way of doing this?!

Posted (edited)

My first thought would be PS:-

 

$UserHomeFldrAD = Get-ADUser -Filter * -Properties HomeDirectory | ?{$_.HomeDirectory -ne $null} | select HomeDirectory

$UserHomeFldrShare = Get-ChildItem -Path \\dev.domain.ac.uk\root\staff\home\a\ -Directory | select FullName

Compare-Object $UserHomeFldrAD $UserHomeFldrShare

 

To be honest its about 5 more lines away from being an automated script......

Edited by HPlum78
  • Thanks 1
Posted (edited)

Doesn't look like it works properly as I've just deleted a home folder that this script claims to be an orphaned folder and it isnt as it is 100% definitely in AD.

 

:(

 

 

[EDIT]

 

I think the 'glitch' must be that it's looking at the username and assuming that's the name of the folder in the home folder share which is OK except where someone has had their username changed (i.e. got married) but their home folder hasn't changed.

Edited by Fazza
Posted
When people get married here, I create a totally new account and move all the contents of their old home folder to their new one to save confusion
Posted

Literally just working on this today. This is my script;

 

$OU = "OU=OU,DC=domain,DC=com"
$rootFolder = "\\server\share\staff"

$users = Get-ADUser -SearchBase $OU -Properties homedirectory -Filter {(Enabled -eq $true)}

$folders = Get-ChildItem -Path $rootFolder

$folders.Count

$liveFolders = @()
$oldFolders = @()

foreach ($folder in $folders.FullName){
   if($users.HomeDirectory -contains $folder){
       write-host $folder -ForegroundColor Green
       $liveFolders += $folder
   }else{
       Write-Host $folder -ForegroundColor Red
       $oldFolders += $folder
   }
}

 

This will create 2 arrays: $livefolders and $oldfolders. You can then use powershell to delete the easily.

 

This looks at the actual homdirectory attribute in AD and doesn't rely on the username matching.

  • Thanks 1
Posted (edited)
Literally just working on this today. This is my script;

 

$OU = "OU=OU,DC=domain,DC=com"
$rootFolder = "\\server\share\staff"

$users = Get-ADUser -SearchBase $OU -Properties homedirectory -Filter {(Enabled -eq $true)}

$folders = Get-ChildItem -Path $rootFolder

$folders.Count

$liveFolders = @()
$oldFolders = @()

foreach ($folder in $folders.FullName){
   if($users.HomeDirectory -contains $folder){
       write-host $folder -ForegroundColor Green
       $liveFolders += $folder
   }else{
       Write-Host $folder -ForegroundColor Red
       $oldFolders += $folder
   }
}

 

This will create 2 arrays: $livefolders and $oldfolders. You can then use powershell to delete the easily.

 

This looks at the actual homdirectory attribute in AD and doesn't rely on the username matching.

 

Is it possible to output the $livefolders and $oldfolders lists to two different text files? I did try but I just got 2 .csv files with lists of numbers in instead of home folder names!!

Edited by Fazza
Posted

It gets worse!

 

Years and years and years ago we had Ranger. Ranger had the home folders like this: \\fileserver\username$ but the new (and more correct way) we do it is \\fileserver\staff$\%username%

 

If I run both the scripts I get 2 different sets of results!!

Posted
When people get married here, I create a totally new account and move all the contents of their old home folder to their new one to save confusion

 

That's actually a really clean way of doing it, whenever I change a name I always miss something.

 

[color=#d4d4d4][font=Monaco][color=#569cd6]$[/color][color=#9cdcfe]homeDriveRoot[/color][color=#d4d4d4] [/color][color=#d4d4d4]=[/color][color=#d4d4d4] [/color][color=#ce9178]"\\server\currentstaffdirectory$\"[/color]
[color=#569cd6]$[/color][color=#9cdcfe]leaversRoot[/color][color=#d4d4d4] [/color][color=#d4d4d4]=[/color][color=#d4d4d4] [/color][color=#ce9178]"\\server\movefolderlocation$\"[/color]

[color=#569cd6]$[/color][color=#9cdcfe]folders[/color][color=#d4d4d4] [/color][color=#d4d4d4]=[/color][color=#d4d4d4] [/color][color=#dcdcaa]Get-ChildItem[/color][color=#d4d4d4] [/color][color=#569cd6]$[/color][color=#9cdcfe]homeDriveRoot[/color][color=#d4d4d4] [/color][color=#d4d4d4]|[/color][color=#d4d4d4] Select [/color][color=#d4d4d4]-[/color][color=#d4d4d4]ExpandProperty Name[/color]
[color=#569cd6]$[/color][color=#9cdcfe]activeUsers[/color][color=#d4d4d4] [/color][color=#d4d4d4]=[/color][color=#d4d4d4]  [/color][color=#dcdcaa]Get-ADUser[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]Filter {Enabled [/color][color=#d4d4d4]-eq[/color][color=#d4d4d4] [/color][color=#569cd6]$true[/color][color=#d4d4d4]} [/color][color=#d4d4d4]|[/color][color=#d4d4d4] Select [/color][color=#d4d4d4]-[/color][color=#d4d4d4]ExpandProperty SamAccountName[/color]
[color=#569cd6]$[/color][color=#9cdcfe]differences[/color][color=#d4d4d4] [/color][color=#d4d4d4]=[/color][color=#d4d4d4] [/color][color=#dcdcaa]Compare-Object[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]ReferenceObject [/color][color=#569cd6]$[/color][color=#9cdcfe]activeUsers[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]DifferenceObject [/color][color=#569cd6]$[/color][color=#9cdcfe]folders[/color][color=#d4d4d4] [/color][color=#d4d4d4]|[/color][color=#d4d4d4] [/color][color=#c586c0]?[/color][color=#d4d4d4] {[/color][color=#569cd6]$[/color][color=#d4d4d4]_[/color][color=#dcdcaa].SideIndicator[/color][color=#d4d4d4] [/color][color=#d4d4d4]-eq[/color][color=#d4d4d4] [/color][color=#ce9178]"=>"[/color][color=#d4d4d4]} [/color][color=#d4d4d4]|[/color][color=#d4d4d4] Select [/color][color=#d4d4d4]-[/color][color=#d4d4d4]ExpandProperty InputObject[/color]
[color=#569cd6]$[/color][color=#9cdcfe]differences[/color][color=#d4d4d4] [/color][color=#d4d4d4]|[/color][color=#d4d4d4] [/color][color=#dcdcaa]ForEach-Object[/color][color=#d4d4d4] {[/color][color=#dcdcaa]Move-Item[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]Path [/color][color=#ce9178]"[/color][color=#569cd6]$[/color][color=#9cdcfe]homeDriveRoot[/color][color=#569cd6]$[/color][color=#9cdcfe]_[/color][color=#ce9178]"[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]Destination [/color][color=#ce9178]"[/color][color=#569cd6]$[/color][color=#9cdcfe]leaversRoot[/color][color=#569cd6]$[/color][color=#9cdcfe]_[/color][color=#ce9178]"[/color][color=#d4d4d4] [/color][color=#d4d4d4]-[/color][color=#d4d4d4]Force}[/color]

[/font][/color]

 

I wrote that script a while ago, it checks AD for active users. If the user doesn't exist or is disabled then it will move their homefolder to a different location, not sure if that will help but it's there anyway. Just change the top to variables.

 

$homeDriveRoot is where the current active staff home folders are.

$leaversRoot is where you want the folders moving instead of deleting just in case.

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