Function Get-WinProfileRebuild($ComputerName) {
    
    Write-Host "`n"

    $AllUsers = Invoke-Command -ComputerName $ComputerName -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock {
        Get-ChildItem -Path C:\Users -Directory -Force -Exclude Public, Default, 'Default User', 'All Users' | 
        Sort-Object -Property LastWriteTime -Descending | Select-Object -First 10 -ExpandProperty Name }
    Write-Host "User Profiles Found (Only displaying newest 10 profiles): $AllUsers"

    Write-Host "`nThis utility will recreate the user's Windows profile.`nThe profile folder will be preserved and renamed to 'username.backup'`n"

    $UserProfile = Read-Host "Enter Username to recreate Windows user profile" 
    $restore = Read-Host "Confirm Rebuild(y/n) or restore(r)"
    if ($restore -eq 'r') {
        Invoke-WinProfileRebuildRestore -Username $UserProfile -Computername $ComputerName
        return
    }
    elseif ($restore -ne 'y') {
        Write-Host "Canceling WinProfileRebuild..." -ForegroundColor Yellow
        return
    }
    $UserSid = get-wmiobject -computername $ComputerName -class win32_userprofile | select-object -property sid, localpath | 
    where-object -property localpath -EQ "C:\Users\$UserProfile" | select-object -expandproperty sid

    $path = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$UserSid"

    $UserName = (Get-CimInstance -Class win32_computersystem -ComputerName $ComputerName).UserName
            
    if ($UserProfile -eq $env:USERNAME) {
        Write-Host "You cannot rebuild your own user profile..." -ForegroundColor Yellow
        Return
    }
    if ($null -ne $UserName) {
                
        if ($UserName -eq $UserProfile) {
            $IsUserNameNotEmpty = 0 #Target user is currently logged in
        }
        elseif ($UserName -eq $env:USERNAME) {
            $IsUserNameNotEmpty = 1 #Can rebuild if you are the one logged into the computer
        }
        else {
            $IsUserNameNotEmpty = 1 #Other user logged in
        }   
                
            
    }
    else {
            
        $IsUserNameNotEmpty = 1 #No User is logged in
           
    }

    if ($null -ne $UserSid) {
                
        $IsSIDNotEmpty = 0 #User Sid exists
            
    }
    else {
            
        $IsSIDNotEmpty = 1 #User Sid not found
            
    }

    Switch ($IsUserNameNotEmpty) {
            
        0 {
            Write-Host "`n$UserName is currently logged in, please have them logout..." -ForegroundColor Yellow
            Write-Host "`n`nScript will now exit, please retry once user has logged off...`n" -ForegroundColor Yellow
            Return
        }
                           
        1 {
                    
            Write-Host "Proceed in rebuilding user profile..." -ForegroundColor Yellow
            Pause
            Break
                    
        }

        Default {
                    
            Write-Host "ERROR WinProfileRebuild.psm1 Line 74" -ForegroundColor Red
            Pause
            Return
        }

    }

    Switch ($IsSIDNotEmpty) {

        0 {
            $GetTime = Get-Date -Format "MMddHHmmss"

            Invoke-Command -ComputerName $ComputerName -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock {
        
                Rename-Item -Path "C:\Users\$Using:UserProfile" -NewName "$Using:UserProfile.backup.$Using:GetTime" -Force -Verbose

                #Write-Host "C:\Users\$Using:UserProfile"
                if (Test-Path "C:\Users\$Using:UserProfile.backup.$Using:GetTime" ) {
                    Write-Host "Rename Successful" -ForegroundColor Green
                }
                else {
                    Write-Host "File rename failed. Please manually rename folder if registry removal succeeds" -ForegroundColor Red
                }
                        

                Remove-Item $Using:path

                Write-Host "$Using:UserProfile removed from registry..."
                Write-Host "User can now relog into the workstation."
            
                        
                Break

            }

        }

        1 {
                
            Write-Host "`nUser SID ID not found in the registry..." -ForegroundColor Red
            Write-Host "`n`nScript will now exit...`n" -ForegroundColor Yellow
            Return   
                
        }

        Default {
                    
            Write-Host "ERROR WinProfileRebuild.psm1 Line 116" -ForegroundColor Red
            Pause
            Break
        }

    }

}

function Invoke-WinProfileRebuildRestore {
    param (
        $Username,
        $Computername
    )

    $FoldersToCopy = @(
        'Desktop'
        'Downloads'
        'Favorites'
        'Documents'
        'Pictures'
        'Videos'
        'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'
        'AppData\Roaming\Microsoft'
        
    )

    #Find Backup folders

    $backups = Get-ChildItem -Path "\\$Computername\c$\Users" | Where-Object -FilterScript { $_ -match $Username }
    Write-Host "Backups"
    if ($null -eq $backups) {
        Write-Host "No backups for $username"
        return
    }
    if ($backups -is [array]) {
        for ($i = 0; $i -lt $backups.Count; $i++) {
            $out = "[{0}] {1}" -f $i, $backups[$i]
            Write-Host $out
        }

        $backupChoice = Read-Host "Choose which backup"
        $backupChoice = [int]$backupChoice
        if (($backupChoice -gt -1) -and ($backupChoice -lt $backups.Length)) {
            $backup = $backups[$backupChoice]

        }     
        else {
            Write-Warning "Invalid Choice - re-run winprofile rebuild to try again"
            return
        }
    }
    else {
        $out = "[{0}] {1}" -f 0, $backups
        Write-Host $out
        $backupChoice = Read-Host "Choose which backup"
        if ($backupChoice -eq 0) {
            $backup = $backups
        }
        else {
            Write-Warning "Invalid Choice - re-run winprofile rebuild to try again"
            return
        }

    }
    
   

    Write-Host "Restoring from $backup"

    $destinationPath = "\\$Computername\c$\Users\$username"
    $sourcePath = "\\$Computername\c$\Users\$backup"
    
    Write-Host "Removing Citrix Shortcuts from backup"
    $CopiedUserDesktop = "$sourcePath\Desktop"
    $Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk


    foreach ($Shortcut in $Shortcuts) {
        $sh = New-Object -ComObject WScript.Shell

        if ($sh.CreateShortcut($Shortcut.FullName).TargetPath -eq 'C:\Program Files (x86)\Citrix\ICA Client\SelfServicePlugin\SelfService.exe') {
            Remove-Item $Shortcut.FullName
        }
    }

    foreach ( $Folder in $FoldersToCopy ) {
        
        $source = Join-Path $sourcePath $Folder
        <# 
        #Special Case for Google Bookmarks
        if($Folder -eq 'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'){
            Write-Host "Restoring Chrome Bookmarks"
            if( -not ( Test-Path -Path $source)){
                Write-Host "No Chrome Bookmarks backup"
                continue
            }
            if( -not ( Test-Path -Path "$sourcePath\AppData\Local\Google\Chrome")){
                Write-Warning "Restoring Chrome bookmarks but Chrome is not installed on this machine"
                
            }
            if( -not ( Test-Path -Path "$sourcePath\AppData\Local\Google\Chrome\User Data\Default")){
                New-Item "$destinationPath\AppData\Local\Google\Chrome\User Data\Default" -ItemType Container | Write-Verbose
            }
            
            Copy-Item -Path $source -Destination $destinationPath -Force -Recurse
            Write-Host "complete"
            continue
        } #>

        if ((Test-Path $source) -and (Test-Path $destinationPath)) {
            Write-Host "Restoring $folder "
            Copy-Item -Path $source -Destination $destinationPath -Force -Recurse
        }
        else {
            Write-Warning "Path Unreachable"
        }
    }

    
}