PowerShell – Backup a list of folders in a text document and zip them with 7zip or PowerShell

List folders in a .txt file and robocopy them to a local location, then zip with 7zip (or native powershell).

#--- DATE VARIABLE
$dateTime = (get-date -f yyyy-MM-dd-HHmm)

#--- VARIABLES
$userDesktop = [Environment]::GetFolderPath("Desktop")
$robocopyFolderList = "$userDesktop\Backup folder list.txt"
$robocopyDestination = "$userDesktop\Backups\Backup_$dateTime"
$zipDestination = "$($env:USERPROFILE)\Desktop\Backups"
$zipFile = "$zipDestination\Backup_$dateTime.zip"

#--- CREATE FOLDERS
    #--- CREATE TEMP ROBOCOPY DESTINATION FOLDER
    if(!(Test-Path -Path $robocopyDestination )){
        New-Item -Force -ItemType directory -Path $robocopyDestination -Verbose
    }    
    #--- CREATE ZIP DESTINATION FOLDER ON DESKTOP
    if(!(Test-Path -Path $zipDestination )){
    New-Item -Force -ItemType directory -Path $zipDestination -Verbose
    }    


Write-Host ""



#--- ROBOCOPY FOLDERS IN LIST

Get-Content $robocopyFolderList | Where {-not ($_.StartsWith('#'))} | foreach { 

    #--- SPLIT THE COMMA DATA FOR VARIABLES
    $itemName = $_.split("{,}")[0]
    $itemDirectory = $_.split("{,}")[1]

    #--- COPY DATA
    if (Test-Path -Path $itemDirectory ) {
        robocopy $itemDirectory $robocopyDestination\$itemName  /MIR /R:0 /W:0
    } else {
        Write-Host "CANNOT FIND $_ " -ForegroundColor Red
        PAUSE
		   }
}


#--- CREATE ZIP FROM ROBOCOPY DESTINATION
#7-zip
#C:\CyberTransport\Applications\7-Zip\7z.exe a -tzip $zipFile $robocopyDestination

#Powershell v3 with .net 4.5
#Add-Type -Assembly System.IO.Compression.FileSystem
#[System.IO.Compression.ZipFile]::CreateFromDirectory($robocopyDestination, $zipFile )

#Powershell v5
Compress-Archive -Path $robocopyDestination -DestinationPath $zipFile



#--- ALL DONE POPUP
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Finished backing up!!",0,"All Done!",0x0)

#--- Open Folders
explorer.exe $zipDestination

 

Leave a Reply

Your email address will not be published. Required fields are marked *