New script: Find Orphaned Home Folders

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.

Share

6 thoughts on “New script: Find Orphaned Home Folders

  1. OleKristianK

    Excellent script! I have some wishes:
    * Some of my user profile folders are named %username%.v2 and %username%.domain, would be great if the script would work on these as well
    * Unfortunatly, some of our systems also have folders within the main userfolder – If I could provide a list of folders to exclude to the script that would be neat. Same problem if you want to move the orphaned folders to a folder within the users-folder.

    Reply
  2. Burri

    Hi there.Are there any News on this question: Some of my user profile folders are named %username%.v2 and %username%.domain, would be great if the script would work on these as well??thanks

    Reply
  3. Burrito

    HiI\\\’ve got a question about your script. Are there any News about %username%.Domain issue??best regardsandreas

    Reply
  4. Darren

    Hi Jaap, love the script it is exactly what we are looking for except our home folders are also used for redirected folders and profiles. I’ve spend 2 days trying to modify your script to takeownership and propagate Admins across all files and folders on the entire orphaned users path, where we can then move the entire users folder without errors on files/folders that we have no access.

    Any tips? I can get the logic to work, but it is just not forcing the ownership of files/folders that we have no access too.

    Cheers
    Darren

    Reply
  5. Tania

    Just wondering if you’ve written any scripts for finding orphaned pages in sharepoint?

    Reply
  6. Jaap Brasser Post author

    Hello Tania,

    I have not written any scripts for that specific purpose, how would you identify a orphaned page in SharePoint then I am sure I would be able to write a script to do that.

    Regards,

    Jaap Brasser

    Reply

Leave a Reply