tools/Get-OrphanedFolders.ps1
function Get-OrphanedFolders2 { <# .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 if (!(test-path $HomeDir)) { Write-Error "Could not find $HomeDir" -ErrorAction Stop } else { Write-Host "Current home directory is $HomeDir" } if (!(test-path $ProfileDir)) { Write-Error "Could not find $ProfileDir" -ErrorAction Stop } else { Write-Host "Current profile directory is $ProfileDir" } # 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 AD Account for HOME folder $($HomeFolder.Name)..." Get-ADUser -Identity $HomeFolder.Name | out-null } catch { Write-Verbose "********** Could not find an AD Account named $($HomeFolder.Name) **********" $OrphanedHomeFolders += $HomeFolder } } # display the orphaned folders that were found if (($OrphanedHomeFolders).count -eq "0") { Write-Output "Could not find any orphaned home folders!" } else { Write-Output "here are the current oprhaned Home folders" $OrphanedHomeFolders | Sort-Object name,lastaccesstime | ft name,fullname,creationtime,lastaccesstime if ($Archive) { $HomeArchiveDir = "$HomeDir\Archive\$Date" New-Item $HomeArchiveDir -ItemType Directory -Force | Out-Null $Confirm = Read-Host "Archive these folders to $HomeArchiveDir ? [y/n]" if ($Confirm -eq "y") { $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 { # Remove last 3 chars to get rid of .v2 / .v6 etc $AccountName = $ProfileFolder.Name.Substring(0,$ProfileFolder.Name.Length-3) # Search for AD Account Name Write-Verbose "Looking for AD Account for PROFILE folder $AccountName" Get-ADUser -Identity $AccountName | Out-Null } catch { Write-Verbose "********** Could not find an AD Account named $AccountName **********" $OrphanedProfileFolders += $ProfileFolder } } # display the orphaned folders that were found if (($OrphanedProfileFolders).count -eq "0") { Write-Output "Could not find any orphaned profile folders!" } else { Write-Output "here are the current oprhaned Profile folders" $OrphanedProfileFolders | Sort-Object lastaccesstime | ft name,fullname,creationtime,lastaccesstime if ($Archive) { $ProfileArchiveDir = "$ProfileDir\Archive\$Date" New-Item $ProfileArchiveDir -ItemType Directory -Force | Out-Null $Confirm = Read-Host "Archive these folders to $ProfileArchiveDir ? [y/n]" if ($Confirm -eq "y") { $OrphanedProfileFolders | Move-Item -Destination $ProfileArchiveDir -Force Write-Verbose "The folders have been successfully moved to $ProfileArchiveDir" } } } } |