Deleting Home Directories

Welcome Forums General PowerShell Q&A Deleting Home Directories

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
2 weeks, 3 days ago.

  • Author
    Posts
  • #126047

    Participant
    Points: 31
    Rank: Member

    Hi,

    I have found that deleting home directories has been a pain and I could really use some help. Here are some steps and issues that I have come across:

    1. Attempt to delete a folder:
      1. Remove-Item bug which requires me to use  gci and pipe to remove-item
      2. Problem with using gci:
        1. does not gather long file names
          1. Have to use a FileSystemObject to read the long file names to delete them
    2. Files in use, found out I have to remove the share first using wmic (Windows 2008)
      1. takes forever
    3. Favorites, Desktop, etc are read only and cannot be deleted until I change the attribute.

    So basically, to delete the home directories, I have to:

    Remove the share

    Take ownership and use icacls to grant administrator full access

    Gather the directory contents and delete them, long file names included

    Change the attributes on the Favorites and Desktop etc to be able to delete them.

    Has anyone already done this in a successful way? It seems as though I have to use Powershell, maybe DOS or Linux commands to get past the long file names, and DOS to remove the attributes?  It seems there must be a better way. I have been struggling with this for a long time and would love some help!

    Thanks!

     

  • #126063

    Participant
    Points: 38
    Rank: Member

    I Use alphafs to get past the long filepath issue.
    however, my process does run under a user context that has full control permission on the home folder paths.

    as well, there was a relatively recent article about changes in windows, with a registry setting, you can completely get rid of the long filepath issues.
    details are here:
    https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file

  • #126065

    Participant
    Points: 66
    Rank: Member

    I've been doing this for AD Users Home Drives and using Remove-Item to remove it, but check out NTFSAccess Powershell Module.

    This has worked well for me to get the FolderSize as we had a bunch that were empty

    https://pastebin.com/Zbu5RyBC

    Import-Module NTFSAccess

    and check out Tech Trainers Tim post on using it.

    Managing NTFS permissions with PowerShell

    What I did was export the list of HomeDrives to an CSV and with some magic created an excel report prior to deleting anything.

     

    $Paths = Get-Content "C:\temp\HomeDrives.txt"
    
    foreach($UserShare in $paths){
    $ShareNames = $UserShare -split "\\"
    
    #The Folder Names are \\namespace.org\dfs\departmenthome\aduser\ so I'm selecting the name of the root share for the name of the report. 
    $DEP_Home = $ShareNames[-2]
    
    $SAVER = "C:\temp\" + $DEP_Home + "-SizeReport.csv"
    $SAVER2 = "C:\temp\" + $DEP_Home + "-SID.csv"
    
    Get-FolderSize -Path $UserShare -SizeType MB | Export-Csv -Path $SAVER -Append -NoTypeInformation 
    Get-NTFSOrphanedAccess -Path $UserShare  | Export-Csv -Path $SAVER2 -Append -NoTypeInformation 
    
    
     }

    You should be able to say

    Remove-NTFSAccess -Path $USerShare the only issue I've found is if the permissions are GenericAll it tends to throw up.

     

  • #126068
    js

    Participant
    Points: 318
    Helping Hand
    Rank: Contributor
    get-wmiobject win32_userprofile | where localpath -eq c:\users\user1 | select localpath
    get-wmiobject win32_userprofile | where localpath -eq c:\users\user1 | remove-wmiobject
    
  • #126092

    Participant
    Points: 31
    Rank: Member

    Thanks All.

    @Jeffery Hayes, I use Get-OrphanHomeFolder to find those folders which no longer have an account associated with it. I modified it to also allow me to check if someone is in a group, such has someone who is an alum but should not have a folder any longer. Maybe that can help you too. I create a csv file from this module before I delete anything also. This also gets the foldersize if you wish.

    I also use Set-NTFSOwner. I checked out the Get-NTFSOrphanedAccess you mentioned which will provide a nice check for me.

    @David Schmidtberge

    Thanks for the references. I will check out the alphafs.

    @js

    These commands are for a local user profile, no?

     

     

     

     

     

    • #126093
      js

      Participant
      Points: 318
      Helping Hand
      Rank: Contributor

      AD users have profiles in the same way.

  • #126173
    Joe

    Participant
    Points: 6
    Rank: Member

    When it comes to deleting files/folders that exceed the character limit, I have been using Robocopy. Mirror an empty directory with the contents of the home directory. That will automatically purge all files.

  • #126582

    Participant
    Points: 31
    Rank: Member

    When it comes to deleting files/folders that exceed the character limit, I have been using Robocopy. Mirror an empty directory with the contents of the home directory. That will automatically purge all files.

    Wow, that's it! Thanks so much!

     

     

You must be logged in to reply to this topic.