Backup your work with PowerShell

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 ‘’ 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.

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.


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.


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

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.

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…



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

Leave a Reply

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