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( [Parameter(Mandatory=$true)] [string]$ProfileDir, [Parameter(Mandatory=$true)] [string]$HomeDir, [switch]$Archive ) Install-WindowsFeature RSAT-AD-PowerShell # 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-Verbose "here are the current oprhaned Home folders" $OrphanedHomeFolders | Sort-Object lastaccesstime | Format-Table name,fullname,lastaccesstime if ($Archive) { Write-Warning "Would you like to arhive these folders?" -WarningAction Inquire $HomeArchiveDir = "$HomeDir\Archive" if (!(Test-Path $HomeArchiveDir)) { New-Item $HomeArchiveDir -ItemType Directory } $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-Verbose "here are the current oprhaned Profile folders" $OrphanedProfileFolders | Sort-Object lastaccesstime | Format-Table name,fullname,lastaccesstime # prompt user if they want to archive them $confirm = Read-Host "Would you like to move these to an archive folder (y/n)" # test for archive folder and create it if it does not exist if ($confirm -eq "y") { $ProfileArchiveDir = "$ProfileDir\Archive\$Date" if (!(Test-Path $ProfileArchiveDir)) { New-Item $ProfileArchiveDir -ItemType Directory } # move all orphaned folders to the archive folder $OrphanedProfileFolders | Move-Item -Destination $ProfileArchiveDir -Force Write-Verbose "The folders have been successfully moved to $ProfileArchiveDir" -ForegroundColor Green } } |