public/Get-OrphanedFolders.ps1
function Get-OrphanedFolders { <# .SYNOPSIS This function will get all home/profile folders and attmept to match them to AD user names Specify the -Archive parameter to have the orphaned folders automatically moved to an Archive directory .NOTES Name: Get-OrphanedFolders Author: Elliott Marter .EXAMPLE Get-OrphanedFolders -ProfileDir D:\Profiles -HomeDir D:\Homes -Archive .LINK https://www.powershellgallery.com/profiles/elliottmarter #> [cmdletbinding(SupportsShouldProcess=$True)] param( [switch]$Archive, [string]$HomeDir = "\\$Env:COMPUTERNAME\Home$", [string]$ProfileDir = "\\$Env:COMPUTERNAME\Profile$" ) $Date = Get-Date -UFormat %Y-%m-%d # Home Folders # create an array to put all folders in $AllHomeFolders = @() # scan through subfolders and add them to the array $AllHomeFolders += (Get-ChildItem $HomeDir -Exclude Archive | Get-ChildItem) # create an array to put orphaned folders in $OrphanedHomeFolders = @() # test each folder name against active directory samaccountnames and if they dont exist place them in the orphaned folders array Foreach ($HomeFolder in $AllHomeFolders) { try { Write-Verbose "Looking for $($HomeFolder.Name)..." Get-ADUser -Identity $HomeFolder.Name | out-null } catch { Write-Verbose "*** Could not find $($HomeFolder.Name) ***" $OrphanedHomeFolders += $HomeFolder } } # display the orphaned folders that were found Write-Output "here are the current oprhaned Home folders" $OrphanedHomeFolders | Sort-Object name,lastaccesstime | Format-Table name,fullname,lastaccesstime if ($Archive) { $HomeArchiveDir = "$HomeDir\Archive\$Date" New-Item $HomeArchiveDir -ItemType Directory -Force $Confirm = Read-Host "Archive these folders to $HomeArchiveDir ? [y/n]" if ($Confirm -ne "y") {throw "Removal aborted..."} $OrphanedHomeFolders | Move-Item -Destination $HomeArchiveDir -Force Write-Verbose "The folders have been successfully moved to $HomeArchiveDir" } # Profile Folders # create an array to put all folders in $AllProfileFolders = @() # scan through subfolders and add them to the array $AllProfileFolders += (Get-ChildItem $ProfileDir -Exclude Archive | Get-ChildItem) # create an array to put orphaned folders in $OrphanedProfileFolders = @() # test each folder name against active directory samaccountnames and if they dont exist place them in the orphaned folders array Foreach ($ProfileFolder in $AllProfileFolders) { try { Write-Verbose "Looking for $($ProfileFolder.Name)..." Get-ADUser -Identity $ProfileFolder.Name.Replace(".V6","") | Out-Null } catch { Write-Verbose "*** Could not find $($ProfileFolder.Name) ***" $OrphanedProfileFolders += $ProfileFolder } } # display the orphaned folders that were found Write-Output "here are the current oprhaned Profile folders" $OrphanedProfileFolders | Sort-Object lastaccesstime | Format-Table name,fullname,lastaccesstime if ($Archive) { $ProfileArchiveDir = "$ProfileDir\Archive\$Date" New-Item $ProfileArchiveDir -ItemType Directory -Force $Confirm = Read-Host "Archive these folders to $ProfileArchiveDir ? [y/n]" if ($Confirm -eq "y") {throw "Removal aborted..."} $OrphanedHomeFolders | Move-Item -Destination $ProfileArchiveDir -Force Write-Verbose "The folders have been successfully moved to $ProfileArchiveDir" } } |