Script to get orphaned home folders and folder size

This script queries AD with the name of the home folder. If this query does not result in an account or a disabled account the script will list the folder size with the folder path and error message.

 
 
 
 
 
4.3 Star
(12)
9,407 times
Add to favorites
Active Directory
9/23/2015
E-mail Twitter del.icio.us Digg Facebook
  • Is it the "True" homedrive?
    4 Posts | Last post September 23, 2015
    • Awsome script - but it does not check if the folder currently in question (assumeing an AD account is found) is the actual homedrive specified for the user.  I come from a multi-site organisation and we keep homefolders at each location rather than centrally.  It is not uncommon for a homedrive to be copied to a new location as the user relocates to a new site and all too often the old copy is left behind.  The script as is would not detect this anomally.  The test for AD account being active is not sufficient - the 'homedirectory' field needs to be tested also.  Could you provide an update to accomodate this?
    • I should add that we use hidden shares in the form \\<servername>\<samaccountname>$
    • Interesting, I see your point. I will include this in the next version, but I will have to take a look at how to best implement this as I will have to build in a check that converts the localpath to the networkpath, probably by using the Win32_Share class or the Wscript.Network object. I will have to take a look how I can best add this into the script.
      
      Thanks for the interesting feedback, I appreciate that!
    • Hello M2Curly,
      
      I have implement the -CheckHomeDirectory switch parameter, when this switch is specified the script will look for the HomeDirectory attribute instead of the samaccountname it normally queries for. Please download the latest version and let me know if this works better for you.
  • Wild cards supported ?
    13 Posts | Last post July 29, 2015
    • HI Jaap
      
      This script is a life saver ! But is there a wildcard that is supported to ignore any .v2 profiles that will obviously not have a user in AD ? I have not been able to find any examples online. When run to just log the output it finds quite a few existing users and I want to ignore those with a .v2 username
      many thanks
    • That is a good request, I could definitely implement this in the next version. I have written a reminder for myself so you can expect this soon.
    • I have updated the script with a RegExExclude parameter, to exclude based on regular expression. Have a look at the following code and let me know if this works for you:
      
      .\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\Server02\Fileshare\Home -MoveFolderPath \\Server03\Fileshare\MovedHomeFolders -ExcludePath '\.v2$' -RegExExclude
    • Hi Jaap
      previously I have ran the below to get a report of all profiles that will be moved
      orphan.ps1 -HomeFolderPath e:\profiles | Format-Table -AutoSize > c:\profiledrives.txt
      
      I have been trying to create the same report, but failing , what would the equivalent be ?
      
    • Are you getting any error? If so please post the complete error code and the exact code you are using to generate the error.
    • HI Jaap
      
      I used to use the cmd below to output to text, it now just say the Format-Table' is not recognized as an internal or external command
      
      orphan.ps1 -HomeFolderPath e:\profiles | Format-Table -AutoSize > c:\profiledrives.txt
    • So what happens when you run this on a different computer, does the command still fail? And how about if you run:
      Get-Process | Format-Table
      
      This seems to be a PowerShell issue, rather than a script issue as the Format-Table cmdlet should always be available. What is the version Windows of the computer you are running this on, can you also post the output of $PSVersionTable here?
    • Hi Jaap
      
      Its been that long since I last looked at this I was having a brain freeze moment and not running the cmd in powershell !
      
      any way I run the cmd and it outputs to a text file all ok, no .v2 folders listed, cmd as below
      orphan.ps1 -HomeFolderPath e:\home -ExcludePath '\.v2$' -RegExExclude | Format-Table -AutoSize > c:\home.txt
      
      it does give back an error though in orphan files as below
      
      Cannot index into a null array.
      At C:\Windows\system32\orphan.ps1:154 char:15
      +     } elseif (([boolean]($ADResult.Properties.useraccountcontrol[0] -band 2))) {
      +               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
          + FullyQualifiedErrorId : NullArray
      
      what does that mean ?
      
      also a question, what is the cmd to list only the .v2 folders to compare with users ?
    • There is currently no way to only display the .v2 folders, is that something you would be interested in having? I have tested the command and I have been unable to reproduce your error.
      
      Can you show me an excerpt of what your folder structure looks like? Which version of Windows and PowerShell are you using?
    • HI Jaap
      
      Powershell version is below, o/s is server 2012 r2 data center edition. folder structure is e:\profiles\profilename
      
      Major  Minor  Build  Revision
      -----  -----  -----  --------
      4      0      -1     -1
      
      
    • I am sorry for the slow response, I have tried to reproduce your problem but I could not generate the same error. Perhaps we are missing something else here.
    • Hi Jaap
      
      thank you for the response, the script works fine so I think we'll leave as is. I have used it fine to move files and folders. many, many thanks
      martin
    • No problem Martin, if you get more information about how I can reproduce this bug in the future I will gladly take a look at this again. Have fun scripting!
  • sAMAccountName different from home folder name
    2 Posts | Last post July 28, 2015
    • Is there anyway to use this script if the sAMAccountName is named differently from the home folder?  I don't control AD in my organization and for some reason the sAMAccountName names are different from how the home folder's are named.  The script as written currently reports many users with no accounts because of this.
    • Currently it looks at the folders and compares the folder name to the samaccountname, which is meant to give you an indication which folders do not match to existing accounts.
      
      It is of course possible to make the script query different properties in Active Directory, can you explain what attributes you would like to compare the folder name to? Then I can have a look how to best implement this into my script.
  • Bug or not?
    2 Posts | Last post July 02, 2015
    • I was trying to use the exludepath feature, but it was not working.  I looked into the code and was confused(I have only limited powershell knowledge).  I modified this:
      
          $ExcludePath | ForEach-Object {
              $ListOfFolders = $ListOfFolders | Where-Object {$_.FullName -ne $_}
          }
      
      to this
      
          $ExcludePath | ForEach-Object {
      	$ExPath = $_
              $ListOfFolders = $ListOfFolders | Where-Object {$_.FullName -ne $ExPath}
          }
      
      There may be a better/cleaner way, but this got it to work as I expected.
    • That is correct, I was still working on that portion of the script and I accidentally uploaded the incorrect version. I have resolved this in version 1.8. Thanks for letting me know wbeavis!
  • 260 character limit
    6 Posts | Last post May 28, 2015
    • Hi
      
      Thanks for taking time to create this great script. I am currently using it to move orphaned home folders to save some valuable disk space. It seems to have run into the 260 character limit as I am seeing the following errors in the console.
      
      Move-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
      At H:\Scripts\Get-OrphanHomeFolder.ps1:104 char:13
      +             Move-Item -LiteralPath $HashProps.FullPath -Destination $HashProps.D ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : WriteError: (a18ca4003deb042...71-a5b8f16d52d2:FileInfo) [Move-Item], PathTooLongException
          + FullyQualifiedErrorId : MoveFileInfoItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
      
      Is there any way to work around this issue?
      
      Many Thanks,
      Mo.
      
    • Yes there is a workaround for this issue, you could use robocopy to manually move these folders for the time being. PowerShell uses the Windows APIs which are bound to the 248/260 character limit for files and folders.
      
      If it would be useful for you I could add in an extra switch parameter in the script that will  use robocopy for moving the files instead of Move-Item.
    • That would be brilliant if you could do that.. Much appreciated :)
    • And it is done, I have tested it and for me it now executed without error, can you let me know how it works for you? Also I added you as an example in the dynamic help in the script ;)
    • Awesome.. You my friend are a true genius :)
      
      Thanks for both additions - I'll test and get back to you in case of any problems.
      
      Cheers.
    • Hello mshajin,
      
      How are the improvements working out for you?
  • Feature
    2 Posts | Last post March 16, 2015
    • Would you be able to add a feature where it would return sizes of users H drives who are currently active?
    • Sure this is possible I have updated my script to include this functionality. It will list both the active and the non-existant or disabled users when using the -DisplayAll parameter. For example:
      
      .\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\Server01\Home -FolderSize -DisplayAll
  • Disabled Users
    2 Posts | Last post March 16, 2015
    • Can you post an example of the script to move folders of disabled users? Folders of accounts with no users move fine but I am unable to moved disabled user folders.
      Thanks
    • Sure I have updated the script and now there is also an example for moving disabled accounts:
      
      .EXAMPLE   
      .\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\Server02\Fileshare\Home -MoveFolderPath \\Server03\Fileshare\MovedHomeFolders -MoveDisabled
      
      Description:
      Will list all the folders in the \\Server02\Fileshare\Home folder and will move orphaned folders and folders that have disabled users accounts to \\Server03\Fileshare\MovedHomeFolders while displaying results to console.
  • Issue when running...
    2 Posts | Last post March 16, 2015
    • Hello! First of all, thank you for sharing this!
      
      I'm having an issue when running it. It lists all home folders and not only the orphaned ones... Somehow it's not comparing with AD...
      
      Thank you very much.
    • Hello SolidSnake,
      
      The script queries based on the foldername and compares it to any known samaccountnames. Do your folder names match the samaccountnames of the users? 
  • Generate a list w/o renaming
    2 Posts | Last post March 16, 2015
    • Thanks for the script. I ran it and it renamed a bunch of valid folders. Not sure why, I have to investigate. I'd like to modify the script to generate a file of the list of orphan folders w/o actually renaming the folders. Can you help me with this? TIA
    • That is strange, if you run the script as stated in the example it should not make any changes. For example:
      
      .\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\Server01\Home -FolderSize
      
      I recommend running the script as such to see if the results match what you are looking for.
  • Cannot move home folders
    2 Posts | Last post July 22, 2013
    • The script identifies 408 orphaned home folders, thank you. The moving of these home folders fails, be it to a new folder in the home folder path or to a share on another server. The error is FullyQualifiedError .... PowerShell Commands MoveItemCommand (set execution policy is RemoteSigned) permissions are not the problem. Could the script be modified to identify each orphaned folder in turn then ask if it should be deleted? I intend to delete them all anyway. Cheers for the good work to date.
    • Could you paste the complete error code, it seems that you cropped out part of the information. Since execution policy is mentioned, could you check if you execution policy is not set too restrictive?
      
      If folder deletion with a confirm prompt is your goal you can run the script in the following manner:
      .\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\DFS\priv$\user | ForEach-Object {Remove-Item -Recurse -Force -Path $_.FullPath -Confirm}
21 - 30 of 37 Items