I am currently working with a customer and the development environment, I have to work in, does not have a direct internet connection so i can’t use GitHub to check-in my work. I have to take a little detour to check it in from my normal workstation. Here is how I do it with PowerShell.

Basically I create an archive of my current workstatus, copy the archive and all my files over to a network share with Robocopy and then I send the archive to my mailbox. I cannot upload these files to my OneDrive, iCloud Drive, Dropbox or whatever, because of firewall rules in place which prevents me from doing so. But this way I always have multiple copies of my work in multiple places and I can go back in time if something went wrong.

Create an archive with PowerShell

There are multiple ways to create an archive with PowerShell

The .NET way

To create an archive with .Net you first have to load the ‘system.io.compression.filesystem’ assembly with the Add-Type cmdlet. This assembly has been introduced in .NET 4.5, so make sure you have at least that version installed.

Then you have to define the source folder and destination .zip file.

$source = 'E:\Scripts' $dest = 'E:\Backup\backup.zip' Add-Type -AssemblyName 'system.io.compression.filesystem' [io.compression.zipfile]::CreateFromDirectory($source,$dest) 1 2 3 4 5 $source = 'E:\Scripts' $dest = 'E:\Backup\backup.zip' Add-Type -AssemblyName 'system.io.compression.filesystem' [ io . compression . zipfile ] :: CreateFromDirectory ( $source , $dest )

By using the CreateFromDirectory method you can create the zip file by adding both variable as arguments.

This will work once, if the destination file already exists you have to either remove the pre existing file, rename it or define a new destination filename. I back up my work daily so I automatically create a new file name each day depending on the current date. If I backup twice or more on the same day I simply rename the backup archive before I create a new one. Here is a complete script on how to do it.

<# Author: Philipp Börner Version: 1.0 Date: 03.01.2016 Version History: .SYNOPSIS This script creates a backup zip archive #> #Define source and destination folders $source = 'E:\Scripts' $dest = 'E:\Backup\' #Getting the current date $date = Get-Date -UFormat _%d_%m_%Y #Building ZIP file name and complete backup path $ZipFile = 'ScriptBackup'+$date+'.zip' $BackupPath = $dest+$ZipFile #creating GUID if ZIP archive already exist, to rename the pre existing file $GUID = [GUID]::NewGuid().ToString() $doubleBackup = $ZipFile+$GUID if (Test-Path $BackupPath) {Rename-Item $BackupPath -NewName $doubleBackup} #Creating ZIP archive Add-Type -AssemblyName 'system.io.compression.filesystem' [io.compression.zipfile]::CreateFromDirectory($source,$BackupPath) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <# Author: Philipp Börner Version: 1.0 Date: 03.01.2016 Version History: .SYNOPSIS This script creates a backup zip archive #> #Define source and destination folders $source = 'E:\Scripts' $dest = 'E:\Backup\' #Getting the current date $date = Get-Date -UFormat _ % d_ % m_ % Y #Building ZIP file name and complete backup path $ZipFile = 'ScriptBackup' + $date + '.zip' $BackupPath = $dest + $ZipFile #creating GUID if ZIP archive already exist, to rename the pre existing file $GUID = [ GUID ] :: NewGuid ( ) . ToString ( ) $doubleBackup = $ZipFile + $GUID if ( Test-Path $BackupPath ) { Rename-Item $BackupPath -NewName $doubleBackup } #Creating ZIP archive Add-Type -AssemblyName 'system.io.compression.filesystem' [ io . compression . zipfile ] :: CreateFromDirectory ( $source , $BackupPath )

The PowerShell v 5 Way

With PoweddrShell v5 you can use the new “Microsoft.PowerShell.Archive” module to work with archives.

The module includes 2 Cmdlets to work with:

“Compress-Archive” – to create archives and “Expand-Archive” to exrtact archives.

Get-Item -Path E:\Scripts | Compress-Archive -DestinationPath e:\Backup.zip -CompressionLevel Optimal -Verbose 1 Get-Item -Path E : \ Scripts | Compress-Archive -DestinationPath e : \ Backup . zip -CompressionLevel Optimal -Verbose

The same “problem” as before applies here as well. The destination archive cannot exist before.

You can easily combine the above .NET script with this. The .NET version is currently a little bit more compatible with “older” environments missing PowerShell 5.

The 7ZIP Way (if you need a password protected archive)

If you have files in your work folder, which will be marked as dangerous by your mail system or spam filter, the previously created ZIP archives will be filtered out and will never arrive. If you create a password protected ZIP file you can send anything you want, if it’s size is allowed by the mail server 😉

To create an archive you need to download or install 7zip on the box your are running the script. You can download it here. the Standalone or fully installation will work for this. I did this with version 9.20 but it should work with the newest version as well. I just extracted the standalone version to a folder in my script folder.

next you need to save this function

function Compress-7ZIPArchive { <# Author: Philipp Börner Version: 1.0 Date: 28.01.2016 Version History: .SYNOPSIS This function creates a new archive #> [CmdletBinding()] param ( [Parameter(Mandatory=$true, Position=0)] [System.String] $Source, [Parameter(Mandatory=$true, Position=1)] [System.String] $Destination, [Parameter(Mandatory=$false, Position=2)] [System.String] $Password, [Parameter(Mandatory=$true, Position=4)] [System.String] [ValidateSet('7z','zip','gzip','bzip2','tar')] $ArchiveType, [Parameter(Mandatory=$true, Position=5)] [System.String] $ZIPPath ) #Testing 7ZIP Path if(!(Test-Path $ZIPPath)){throw '7ZIP could not be found'} #Creating arguments for archive creation $arguments = "a -t$ArchiveType ""$Destination"" ""$Source"" -mx9" if($password){$arguments += " -p$password"} $ZipUp = Start-Process $ZIPPath -ArgumentList $arguments -Wait -PassThru -WindowStyle Normal if($ZipUp.ExitCode -EQ 0){ Write-Output "succesfully created archive $Destination" } elseif($ZipUp.ExitCode -ne 0){Write-Output 'creation of archive failed'} } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 function Compress -7ZIPArchive { <# Author: Philipp Börner Version: 1.0 Date: 28.01.2016 Version History: .SYNOPSIS This function creates a new archive #> [ CmdletBinding ( ) ] param ( [ Parameter ( Mandatory = $true , Position = 0 ) ] [ System . String ] $Source , [ Parameter ( Mandatory = $true , Position = 1 ) ] [ System . String ] $Destination , [ Parameter ( Mandatory = $false , Position = 2 ) ] [ System . String ] $Password , [ Parameter ( Mandatory = $true , Position = 4 ) ] [ System . String ] [ ValidateSet ( '7z' , 'zip' , 'gzip' , 'bzip2' , 'tar' ) ] $ArchiveType , [ Parameter ( Mandatory = $true , Position = 5 ) ] [ System . String ] $ZIPPath ) #Testing 7ZIP Path if ( ! ( Test-Path $ZIPPath ) ) { throw '7ZIP could not be found' } #Creating arguments for archive creation $arguments = "a -t$ArchiveType " "$Destination" " " "$Source" " -mx9" if ( $password ) { $arguments += " -p$password" } $ZipUp = Start-Process $ZIPPath -ArgumentList $arguments -Wait -PassThru -WindowStyle Normal if ( $ZipUp . ExitCode -EQ 0 ) { Write-Output "succesfully created archive $Destination" } elseif ( $ZipUp . ExitCode -ne 0 ) { Write-Output 'creation of archive failed' } }

and run it with this command to create a .7z archive. This simply defines a source, a destionation archive, an archive type and the path to the 7za.exe file (you can also point to the “normal” 7z.exe if you have installed it.

Compress-7ZIPArchive -Source C:\Temp -Destination E:\backup\temp.7z -Password 1234 -ArchiveType 7z -ZIPPath "E:\Scripts\7ZIP\7za.exe" 1 Compress -7ZIPArchive -Source C : \ Temp -Destination E : \ backup \ temp . 7z -Password 1234 -ArchiveType 7z -ZIPPath "E:\Scripts\7ZIP\7za.exe"

You can also create the following archiv types as well: ‘7z’,’zip’,’gzip’,’bzip2′,’tar’. 7Zip also creates .iso and .udf archives but those are not working with my arguments. I’ll may add those later…

Conclusion

These are some ways, to create a backup for your scripts, if you don’t have access to your GitHub or you simply want to create a local backup of your work, should cover you for the future. I started to use these scripts in my daily routine to backup my scripts in the evening and update them if needed in a different environment using Robocopy. I also use some versioning during the day by using ISE-Steroids. And as always, keep in mind to create at least daily backups of your scripts!

If you have any questions or other ideas on how to handle this post in the comments.

Cheers Philipp