PC Backup In Two Lines

Author: Ethan Glover
Last Updated: Nov 14, 2021, 8:56 AM UTC
PowerShell script to download and run another script.

I backed up my entire PC setup with two lines of PowerShell code that I store on a USB.

Start-BitsTransfer -Source https://raw.githubusercontent.com/eglove/PSScripts/master/pcSetup.ps1 -Destination script.ps1

Invoke-Expression‘.\script.ps1’

Maybe not exactly two lines of code. Maybe it's two lines to download the actual script which happens to be around 144 lines. I wouldn't want to store that on a USB for obvious reasons. If I update it, it's not up to date on the USB. And I'm definitely not going to bother to update it in two places.

So what exactly does the actual script do? Well, the first step is the most important step and the one that made me realize I could write this script, to begin with. It installs Chocolatey Pro including copying over the license file that's also kept on the same USB.

Second, while I'm there, I may as well set up WSL 2 and Ubuntu. The tricky part I realized during the process of writing this is that even though chocolatey adds its own environment variables, as well as the variables of the software it will later install, that's not available in the current session of PowerShell. This also means the choco command isn't available in that current session.

So, after downloading and installing WSL I had to open up a new PowerShell window, install it via Chocolatey, then start Ubuntu so that I could set up a new user. Every subsequent choco or GitHub CLI command had to be run in its own PowerShell instance.

With that done, it's just a matter of installing all the software I have listed in an array via Chocolatey. As well as any PowerShell modules I use, and yarn global installs.

And I couldn't forget to download, install, and set an environment variable for all my other scripts. But I couldn't just download them. I had to git clone them. Via the GitHub client. This means I had to install gh cli and authenticate it before moving on. This sets me up for GitHub commands in the future.

But why stop there? Cortana is still active. My toolbar isn't set up as I like it. That can all be done by changing Registry settings. Which can be set via PowerShell. So I applied any Windows settings I wanted to save.

And while I have a script to update software, I allow the JetBrains toolbox to handle IDE updates and generate shell scripts. So I updated those settings as well as the theme and any other settings I have set on IntelliJ. And settings for terminus, can't forget to backup my terminus settings.

All wrapping up in a clean up process by doing an initial windows update for the clean install, removing any desktop shortcuts generated by the choco installs, running cleanmgr to allow a safe delete of Windows.old and deleting the downloads for WSL and the initial script off the USB.

I love this little script. Everything backed up in just the way I want it, ready for a fresh Windows install at any time. I honestly wish I had an excuse to run it more often.