functions/Restore-Backup.ps1

function Restore-Backup(
    [string] $Path = ".",
    [string] $BackupPath,
    [Switch][bool] $Silent = $false
) {
    $path = (get-item $path).FullName
    $projectName = split-path -Leaf $Path    
    $latestBackup = $null

    if (![string]::IsNullOrEmpty($BackupPath)) {
        $files = Get-ChildItem $BackupPath | where { ! $_.PSIsContainer }
        if ($files.Length -ne 0) {
            #seems like a backup leaf folder, copy all
            $latestBackup = $BackupPath
            write-host "manual backup folder = $latestBackup"
        }
    }
    else 
    {
        $BackupPath = Join-Path $Path "..\_backup\$projectName"
    }

    if ([string]::IsNullOrEmpty($latestBackup)) {
        write-host "looking for latest backup in folders:"
        $folders = gci $BackupPath `
            | ? { $_.PSIsContainer } `
            | Sort-Object -Descending -Property LastWriteTime

        $folders
        $latestBackup = $folders[0].FullName

        write-host "latest backup = $latestBackup"
    }

    $go = $true
    write-host "restoring backup from $latestBackup"
    if (!$Silent) {
        $response = Read-Host -Prompt "Do you want to restore backup from $latestBackup to $Path? ([Y] to confirm)" 
        if ($response -match "[Y|y]") {
            $go = $true
        } else {
            $go = $false
        }
    }

    if ($go) {
        Copy-Item $latestBackup\* $Path -Recurse -Verbose -Force
        write-host "backup from $latestBackup restored"
    } else {
        write-host "user aborted"
    }
}