public/Get-OrphanedFolders.ps1

function Get-OrphanedFolders {


    [cmdletbinding()]    
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProfileDir,
        [Parameter(Mandatory=$true)]
        [string]$HomeDir
    )


    $Date = Get-Date -UFormat %Y-%m-%d
    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-Host "here are the current oprhaned Home folders"

    $OrphanedHomeFolders | 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") {

        $HomeArchiveDir = "$HomeDir\Archive\$Date"

        if (!(Test-Path $HomeArchiveDir)) {

            New-Item $HomeArchiveDir -ItemType Directory

            }

    # move all orphaned folders to the archive folder
        $OrphanedHomeFolders | Move-Item -Destination $HomeArchiveDir -Force
        Write-Host "The folders have been successfully moved to $HomeArchiveDir" -ForegroundColor Green

    }

    ###########################
    ##### 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-Host "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-Host "The folders have been successfully moved to $ProfileArchiveDir" -ForegroundColor Green

    }

}