This is version 2 to below Powershell script to query parent directory containing user home folders or profiles.
Especially helpful for housekeeping deprecated RDS & Citrix profiles, maximising disk space on your profile file server.
Improvements
- Added error handling with try/catch.
- Added input for directory path.
- Added color for deprecated profiles.
- Removed unnecessary reporting of valid profiles.
$moduleName = Get-Module -List ActiveDirectory if(!$moduleName) { Write-Host "Can't locate ActiveDirectory PowerShell module. Try running script from a server with AD/Domain role." -foregroundcolor "magenta" exit } $folders = Read-Host -Prompt "Enter UNC path containing profiles. (Eg. \\ServerName\D$\CitrixProfiles)" $folders = gci $folders #This is the root path containing home folders. foreach ($folder in $folders) { Try { $username1 = $null $username1 = (get-aduser $folder.name).Name if ($username1 -ne $null) { #Write-Host "$folder This user exists, so don't delete the home folder" } }#End Try Catch { Write-Host "THIS PERSON NO LONGER EXISTS: $folder" -foregroundcolor "magenta" #Remove-Item $folder.fullname -Force -Recurse #Un-Comment above line if you wish to delete profiles following confirmation of non-existence. } }#End foreach