I recently uploaded a new script, Get-OrphanHomeFolder.ps1 to the TechNet Gallery. The script is designed to get a list of all folders in a path and for each of those folders it will query AD to verify if there is a matching Sam account. If this property is not found the script considers this to be an orphaned home folder. If the -FolderSize property is specified the script will retrieve the size of the orphaned folder and display the results as an array of objects.
The script is available here: Technet Gallery: Get-OrphanHomeFolder.ps1
This portion of the script will grab the folder name and query AD for a matching samaccountname, to do this I utilize the [adsisearcher] accelerator so there is no dependency on the AD Cmdlets in this script:
49 50 | $CurrentPath = Split-Path $_ -Leaf $ADResult = ([adsisearcher]"(samaccountname=$CurrentPath)").Findone() |
If a matching account is not found the script will display the error, in this case the ‘Account does not exist and has a home folder‘ message and the full path to the folder. This information is stored in a hashtable. The code for that look like this:
54 55 56 57 | $HashProps = @{ 'Error' = 'Account does not exist and has a home folder' 'FullPath' = $_.FullName } |
The information gathered in this hash table is then used to build a custom PowerShell object with the hash table as its properties:
65 | New-Object -TypeName PSCustomObject -Property $HashProps |
For more information about this script or any of the other contributions, drop me a line and I will be happy to discuss this further.