public/Optimize-Homes.ps1

Function Optimize-Homes {

    #Requires -RunAsAdministrator

    [cmdletbinding(SupportsShouldProcess=$True)]
    
    Param(
        
    
        [Parameter(Mandatory=$true)]
        [string]$HomeDirectory,

        [string]$LogDirectory = "$HOME\Desktop"

    )

    if ((Test-Path $HomeDirectory) -eq $False) {
        Write-Warning "Home directory not found" -WarningAction Stop
        }else{
        $HomeDirectory = (Get-Item $HomeDirectory).fullname
        }

    # Create a migration logs directory if it doesnt already exist
    $LogPath = "$LogDirectory\Data Migration Logs"
    if (-not(test-path $LogPath)) {
        new-item -Path $LogPath -ItemType Directory
        }
    
    # Warn user and check
    Write-Host "User Home Directory: $HomeDirectory" -ForegroundColor Green
    Write-Host "Log Path $LogDirectory" -ForegroundColor Green
    Write-Host "Home directory should NOT end in Home$, it should point to the Office, Student or Staff folders WITHIN Home$" -ForegroundColor Yellow

    Write-Warning "Please confirm these paths are correct" -WarningAction Inquire

    #Remove all temp and junk files in home dirs
    Write-Host "Removing temp and junk files from user homes..." -ForegroundColor Yellow
    Get-ChildItem $HomeDirectory -Recurse -Attributes h,s -Include '$RECYCLE.BIN'  | Remove-Item -Force -Recurse
    Get-ChildItem $HomeDirectory -Recurse -Include ('desktop.ini','*.tmp','*.log','*.bak','*.old','*~$*.doc') | Remove-Item -Force -Recurse

    # Get all the user folders in the specified directory
    $UserHomes = Get-ChildItem -Path $HomeDirectory | Where-Object { $_.PSIsContainer }

    # Loop through each folder and copy everything across
    foreach ($Folder in $UserHomes) {

        #Generate username based on new home folder
        $Username = $Folder.Name
        
        #Get fullname of folder so Robocopy doesnt get grumpy later on
        $UserHome = $Folder.Fullname
        
        #Create the log file
        $LogFile = "$LogPath\$Username.txt"

        # Try to create new folders if they dont exist
        New-Item -Path "$UserHome" -Name Desktop -ItemType Directory -ErrorAction SilentlyContinue
        New-Item -Path "$UserHome" -Name Documents -ItemType Directory -ErrorAction SilentlyContinue
        New-Item -Path "$UserHome" -Name Pictures -ItemType Directory -ErrorAction SilentlyContinue
        New-Item -Path "$UserHome" -Name Videos -ItemType Directory -ErrorAction SilentlyContinue
        New-Item -Path "$UserHome" -Name Music -ItemType Directory -ErrorAction SilentlyContinue
        New-Item -Path "$UserHome" -Name Favorites -ItemType Directory -ErrorAction SilentlyContinue

        # Set Robocopy Params
        $RC_Parameters = @(
        '/MOVE'
        '/E'
        '/W:0'
        '/R:0'
        '/NP'
        '/MT'
        "/log+:$LogFile"
        )

        Write-Host "Now migrating $Username's data..." -ForegroundColor Yellow

        Robocopy "$UserHome\My Documents" "$UserHome\Documents" $RC_Parameters
        Robocopy "$UserHome\My Pictures" "$UserHome\Pictures" $RC_Parameters
        Robocopy "$UserHome\My Videos" "$UserHome\Videos" $RC_Parameters
        Robocopy "$UserHome\My Music" "$UserHome\Music" $RC_Parameters
        Robocopy "$UserHome\My Favorites" "$UserHome\Favorites" $RC_Parameters
        Robocopy "$UserHome\My Favourites" "$UserHome\Favorites" $RC_Parameters

        Write-Host "$Username's data has been successfully migrated!" -ForegroundColor Green

    }

}