Fazza Posted May 16, 2018 Posted May 16, 2018 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?!
HPlum78 Posted May 16, 2018 Posted May 16, 2018 (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 May 16, 2018 by HPlum78 1
Fazza Posted May 16, 2018 Author Posted May 16, 2018 Scripts I use Orphaned Folder Check 1.1 Just tried and it worked like a dream! 398 folders to delete!
Fazza Posted May 16, 2018 Author Posted May 16, 2018 (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 May 16, 2018 by Fazza
caffrey Posted May 16, 2018 Posted May 16, 2018 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
DavidYoung Posted May 16, 2018 Posted May 16, 2018 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. 1
HPlum78 Posted May 16, 2018 Posted May 16, 2018 Yeah I am with @DavidYoung like I said 5 lines off automation :-P
Fazza Posted May 17, 2018 Author Posted May 17, 2018 (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 May 17, 2018 by Fazza
Fazza Posted May 17, 2018 Author Posted May 17, 2018 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!!
ShellfishClive Posted May 17, 2018 Posted May 17, 2018 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.
tim7777 Posted May 23, 2018 Posted May 23, 2018 Try this Compare folder list to AD user list - PowerShell for Windows - Ask the Experts - IDERA Community
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