Cleaning Up Home Folders with PowerShell

If you use home folders and/or folder redirection, you probably are wasting some space! When a user in AD is deleted, their documents will still remain on your server. To reclaim this space, we can use a very simply PowerShell script:

PowerShell script:

==============================================

Add-PSSnapin Quest.ActiveRoles.ADManagement

$Folders = Get-ChildItem ‘\\SERVER\SHARE’

$Users = Get-QADUser -SizeLimit 100000

$StaffUDriveOnly = Compare-Object $Folders.Name $Users.SamAccountName |
Where-Object {$_.SideIndicator -eq “<=”}

$InputObjects = $StaffUDriveOnly.InputObject

foreach ($InputObject in $InputObjects) {

Remove-item “\\SERVER\SHARE\$InputObject”
-Recurse -Force -WhatIf

}

=====================================================

This script will compare all of the folder names in on your share against all of the users retrieved by the Get-QADUser command. It will then filter and show you any folder that exists on the share that doesn’t have a corresponding user in AD. Finally, we use Remove-Item to delete these folders and to reclaim some space!

Share