I use several physical PCs and virtual machines. I use OS X and Windows. I (sometimes) write PowerShell scripts to perform various tasks.
This isn’t an ideal scenario for effectively writing scripts and managing versions. It’s only recently occurred to me that I could be managing my scripts smarter that I have been. Not being a developer or needing to write code daily, I’ve gotten by, manually versioning scripts and syncing scripts across machines via Dropbox etc.; however I’ve always known that there are better ways.
There are two tools that I’ve recently added to my toolkit that is now making it more effective to manage PowerShell scripts across various machines, versioning those scripts and even allowing me to write PowerShell scripts on OS X:
- Visual Studio Code – this tool has made coding far more enjoyable
- GitHub – I’ve been aware of Git and GitHub for some time but not taken advantage of GitHub until recently
In this article, I’ll briefly cover using these tools for use with PowerShell.
Visual Studio Code
Visual Studio Code is a cross platform code editor from Microsoft which I’ve come to really enjoy using because it allows me to edit PowerShell code directly on a MacBook (my primary machine). It supports Windows, OS X and Linux and features extensions that provide support for various languages, themes and features, such as integration into other tools etc.
Providing the same experience across OS X and Windows is fantastic and it’s great that Microsoft is embracing platforms other than Windows. While I can’t execute PowerShell scripts on OS X, the ability to edit and manage code on my primary OS simplifies my workflow.
Visual Studio Code provides as consistent UI across platforms – here it is on OS X with support for PowerShell added via an extension:
And here’s the same view on Windows:
Integration with PowerShell
VSCode supports extensions with which we can enable PowerShell syntax support, so even on OS X, we can write PowerShells scripts. To install the PowerShell extension, press F1, type
ext install and press Enter. Select to install the PowerShell extension and restart Visual Studio Code when prompted.
Updating extensions is handled in the same manner:
There’s plenty of extensions and themes to customise VSCode to make the tool work for you.
There’s most likely plenty of methods for managing PowerShell code; however a tool such as GitHub which provides a free option is very popular and is actually used by the PowerShell team at Microsoft, making it a desirable solution.
Getting Started with GitHub
To start with GitHub create a repository, you’ll be prompted to also add a readme file and choose a licence. It’s quite simple and GitHub will walk you through the process. I created a specific repository to support my PowerShell scripts.
The GitHub Desktop application makes it simple to manage code repositories on GitHub and can be used to synchronise code across multiple machines. This too supports multiple platforms (OS X and Windows), so we get the same experience on both platforms.
For a Windows application,
To create a local copy of your repository, Clone the repository via File / Clone Repository or as in the screenshot below:
You’ll be asked where to store the local copy. In my instance I’ve chosen to store code in \Documents\WindowsPowerShell, so I get a sub-folder in that location to store my scripts.
You could create a repository called WindowsPowerShell and store the local copy in \Documents, so that your entire PowerShell home folder is managed in GitHub.
Integrating Git with Visual Studio Code
Using a source code management solution is still new to me; however my own requirements for managing PowerShell scripts are simple and I can’t see a need to use Git features such as branching. Using GitHub does give me great features such as change tracking which are suitable for anyone writing simple scripts.
Visual Studio Code provides direct integration itself with Git, so that you can commit changes and sync code right from within VSCode. The Git command-line tools are required which can be installed via Chocolatey on Windows or Homebrew on OS X. Unfortunately on Windows, installing Git via Chocolatey didn’t work for me, so I’ve used the Git tools directly from the Git website.
Once installed you can manage your repository in VSCode by opening the folder containing your locally cloned copy of the repository and you can commit and sync without leaving VSCode.
My preference is still to use the GitHub Desktop applications due to the way that changes are presented and the ability to compare differences in scripts.
With GitHub and Visual Studio code, I’ve found a method that allows me to edit PowerShell code on multiple PCs and platforms with a consistent user experience and enables versioning. I’ve only scratched the surface with what I can do with these tools but I’m very happy with how this is working out so far.