As many of you probably know I do a lot of PowerShell scripting when time allows it, and after scripting, for many years I finally saw the need for source control. Source control not only ensures that I can revert back to a previous version of my code, but it also provides me with a single place to find all my code. I am using source control for both my personal work (like Citrixlab.dk and helping others out) and also for my professional work where I use a shared repository with my colleagues.
For my personal code, I actually have two source control systems, one is the public one for my sharing, this is GitHub, and the other repository is in Azure DevOps. Both work well, but I like to have the separate so that I have the code I am working on in my private repository and my published in the public one. I know I can use just one and mark the code as private or public, but I like the fact that they are separate.
So in this article, I will guide you through the setup of Visual Studio Code, GitHub and finally how to use a code signing certificate. The process is fairly simple, but I will do this guide anyway and hopefully do another blog post on more extended use of Visual Studio Code and GitHub.
Let’s get started.
Installing Visual Studio Code
Go to this website and download Visual Studio Code: https://code.visualstudio.com
Unblock the downloaded file by right-clicking the file, click “Properties”
Now mark the “Unblock” checkbox and hit “OK”
Run the file to start the installation process. Hit “Next”
Accept the EULA and click “Next”
Choose the installation directory. I have chosen to use the default location. Click “Next” to continue
Name the start menu folder. I have chosen to use the default. Click “Next”
Choose the additional tasks that fit your needs. I have chosen to mark all the options under “Other” this is because I use Code as my default edition. Click “Next”
Review the selected configuration and click “Install” if you are satisfied
The installer will do its work.
When the installation is completed make sure that the checkbox is marked so that Code will open up so we can do some work in there. Click “Finish” to complete.
When Code is open I want to install some extensions, this is done by clicking on the icon marked on the picture.
Now search for “PowerShell” as shown and click on “Install”
Next search for “Bracket” and click on “Install”. This is just an extension that I like to use so you can skip it if you don’t want it.
Now close Visual Studio Code so we can move on to installing “Git for Windows”
Installing Git for Windows
Git for Windows is needed to be able to fetch and push code into GitHub. To install this we first need to download it and you can get it from this URL: https://code.visualstudio.com
After the download completes perform the same unblocking steps as we did in the section about Visual Studio Code. Now right click the file and choose “Run as administrator” and click “Yes” to run the file
For the installation, I choose all the defaults, but let’s take it one step at the time. Click “Next” on the public license information.
Choose the installation folder you want to use
Choose the components, I haven’t changed anything on the selection as shown below. Click “Next”
Name the folder in the Start menu and click “Next”
Choose the default edition to be used and click on “Next”
Select the adjustments to the Path environment and click “Next”
Choose the HTTPS transport backend and click “Next”
Select the checkout style and click “Next”
Choose the terminal emulator and click on “Next”
Make your selection for extra options and click “Install”
Let the installer do its magic
Clear the checkboxes and click on “Finish”
Now we are all set to make use of Git inside of our Visual Studio Code application.
Setting up GitHub and using it in Visual Studio Code
In this section, I will show you how to make a repository in GitHub then how to use that repository inside of Visual Studio Code.
The first thing we need to do is go to https://www.github.com and if you don’t have a user create this. I already have my user so I will just sign in.
I will sign in using my username and password and then click “Sign in”
On the left side go under “Repositories” and click on “New”
I will fill out the fields as shown below calling my new repository “Citrixlab-Demo” and finish off by clicking “Create repository”
After clicking “Create repository” I will get to a page showing me the URL on the repository and I will use this in Visual Studio Code to clone the repository.
Open Visual Studio Code again, click on “View” and then “Command Palette” or press Ctrl+Shift+P.
Type in “Git: Clone” and press “Enter”
Now enter the URL of the Git repository that was created before and press “Enter”
I have created a folder on my C-drive called “Git” which will hold all my repositories. A subfolder will be created by the Git provider so the folder containing the actual code files will be called “Citrixlab-Demo” just like my repository.
When finished cloning the repository Visual Studio Code will ask you if you want to open the repository. Click on “Open Repository” to do this.
Since my repository is empty there are no files to show, but this is what it will look like.
So let’s create the first file by clicking on “File” and then “New File” or just press Ctrl+N
I wrote a small PowerShell code snippet as shown below, and as you can see the file is not yet named or saved.
So click on “File” and then on “Save” or press Ctrl+S
Choose the “PowerShell” as the extension and name your file. I have chosen the name “Test.ps1” as my file name. Click on “Save”
Now that the file is saved and Visual Studio Code knows that it is a PowerShell file, you will see that the coding window now colorized the code so it is pretty to look at.
On the left side click on the icon as shown below. This is the source control part of Visual Studio Code.
Provide a meaningful text for the reason you are pushing code to the source control (GitHub). I always start with “Initial push” and all the following will have a text saying what has changed since last push. Click on the check mark when you want to commit the change.
If you haven’t staged your changes you will be asked the question below. Click on “Yes” to stage the changes.
Now you would probably think that everything is good and your code is safely pushed to the repository, but that is actually not the case. You will need to tell Visual Studio Code to do the push and that is done by click on the three “…” in the corner and then click on “Push”
Since we are using GitHub it will ask you for your credentials, so type them in and click on “Login”
When the code is pushed to the repository you will be asked if Visual Studio Code should automatically run a “Git fetch” periodically. Answer “Yes” to this question.
Now the code is pushed to the repository, so let’s go back to the GitHub website and see if it has been updated. If you click on the “Code” section in the top you should see that “Test.ps1” is now in the online repository.
Now we have a fully working setup using Visual Studio Code, Git for Windows and GitHub as the repository. This is essential for moving on with keeping your code safe and versioned. You can create all the files you need and you can also create subfolders so for instance, you can create a subfolder called “Tests” which contains all your Pester tests for your code. We are now ready to move on to the code signing part that requires a certificate.
Signing your PowerShell code with a code signing certificate.
To perform this part of the guide you need to have a code signing certificate installed on your machine, if you don’t have one you can buy one at https://www.digicert.com/code-signing the service at digicert is great!
When you have it installed the code signing is very easy. Lets take a look at which certificates is available for code signing on your PC, to do this open Visual Studio Code, go to the terminal and type in “Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert”. It should look like the screenshot below
To sign your PowerShell script we need to put the certificate into a variable so that we can use it afterwards. In the script section put the following in line 1 “$cert=Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert”. On line 2 we type in the code that actually signs the PowerShell script, so type in the following “Set-AuthenticodeSignature -FilePath C:\GIT\Citrixlab-Demo\test.ps1 -Certificate $cert”. When those two lines are written we just need to execute the code by pressing F5. This should give you the output below.
If we now open our test.ps1 file it should look like this.
If we check our test.ps1 script for code signing now with this command “Get-AuthenticodeSignature .\test.ps1” we should get something similar to the picture below.
So this became quite a long guide, but I feel it is important to go through all these steps first so that when we get into more complex setups with coding in Visual Studio Code and PowerShell we have a common understanding of the setup of the system. What we have now is a fully functional setup with Visual Studio Code, GitHub source control and a code signing certificate so that we can sign the code we publish to GitHub or maybe even to the PowerShell gallery.
I hope this guide is of value to you, and as always reach out if you have feedback on the article. If i missed anything let me know.