Getting Started with PowerShell
I haven't been the most enthuasiastic user of command lines in the past. I dabbled here and there but I've mostly used GUIs whenever possible. Most of the time, this didn't have any negative effects on my productivity.
When I started using Azure however, there were a number of tasks that were not easily performed using the old portal. Reading online, I found that the tasks could be run more quickly if I used PowerShell. I found out that GUIs are a great way of finding out what a tool is capable of by clicking around and seeing what is available.
Once the process to perform a task is learned there aren't any other performance improvements that can be made when using the GUI. You can then look at automating the task using something like PowerShell which could potentially turn a number of clicks into just one.
I've been developing HTML5 apps and REST APIs but have become more interested in DevOps to automate the release process. We are already using Visual Studio Team Services so I have been creating Build and Release definitions. These require some customization which has led me down the PowerShell path.
Naturally the potential for making my job easier in the long run was very appealing to me. I began automating daily tasks where I could but this was only when I had down time from other projects (which could be months between these opportunities) and it always took me some time in order to get my head into the syntax and thought processes related to PowerShell.
I'm writing this to add some personal documentation so that I can cut down on the time spent relearning the language and hopefully write better scripts in the future.
Notes
I'm using Windows 10 and PowerShell 5. I'll go through getting the version of PowerShell on the machine further down. Most of the stuff in this series will work in older versions but Microsoft is updating and improving PowerShell all the time, including getting it to work on Mac/Linux so keep an eye out for updates.
You can follow the PowerShell team on Twitter and check out the repo on GitHub or read the documentation here.
Setting up the environment
Setting up the console app
I don't norrmally use the PowerShell console app but it can be a good idea to get a feel for it. There are some versions of Windows that don't have the GUI available so you'll only have access to the console app. I normally use the PowerShell Integrated Scripting Environment (ISE), Visual Studio or VS Code.
Using Windows 10, you can find PowerShell console app in the Start menu. There are a couple of ways to do this
- Search
- Open the Start menu and type PowerShell
- Choose "Windows PowerShell" from the list presented
- Direct
- Open the Start Menu
- Go to All Programs > Accessories
- Click on Windows PowerShell
Note
Make sure you're using the correct version. I'm using a 64-bit version of Windows so the icon with "x86" in the title is the 32-bit version. It's a good idea to pin the 64-bit concole app and the ISE to the Start menu at least. I've also pinned them to the Task bar for easier access.
If you click in the upper left corner and select "Properties" then you will be given a window that will allow you to change the the appearance of the console app to fit your preferences.
It's a good idea to adjust the font size to something comfortable. On the "Layout" tab, set both Widths to the same value. If you don't you may get a horizontal scrollbar at the bottom of the screen which can be a problem when writing longer scripts.
Another tip I picked up is that you can set up more than 1 shortcut on the desktop and format each window launched from the shortcut to appear differently. This is useful if you're running more than 1 script at a time and want to easily tell which is which.
After creating a shortcut you can right-click on it and click on the Properties to get a similar window to alter the formatting of the console.
An important thing to note is that a lot of the time you'll need to have the Administrator privileges turned on in order to have the cmdlets to run. You can turn them on automatically for a shortcut by checking the "Run as Administrator" checkbox by clicking on the "Advanced" button on the Shortcut tab in the Properties window.
Setting up the Integrated Scripting Environment (ISE)
You can find the ISE in the same place as the console app. You can also start the ISE from the console app. A few ways to start the ISE are
- Search
- Open the Start menu and type PowerShell
- Choose "Windows PowerShell ISE" from the list presented
- Direct
- Open the Start Menu
- Go to All Programs > Accessories
- Click on Windows PowerShell ISE
- Console App
- Enter "ise" and press Enter
The white section on top is called the Script Pane as is where you'll spend most of your time wrtiting scripts.
The blue section on the bottom is the Console Pane and works in a similar manner to the Console window. You can type sommands directly into it and run them. When you run code in the Script Pane the output will be displayed in the Console Pane.
The section on the right is the Command Add-On. You can search through the available cmdlets and also get parameters and documentation using the add-on. This is very useful when learning what are the possible options for a cmdlet.
Getting the Installed PowerShell Version
In the console app or ISE you can use the cmdlet
$PSVersionTable
to see what version of PowerShell is installed on your machine. This is what is installed on my machine.
Other Editors
You aren't limited to using the ISE for writing PowerShell scripts. You can use any editor and as long as the files are saved using the .ps1 extension they can be run as PowerShell.
When testing out scripts at the beginning I normally start out using the ISE but I also use Visual Studio or Visual Studio Code. They both have some pretty useful extensions that aid in writing scripts.
I have the PowerShell extension installed for VS Code from the Visual Studio Marketplace.
I have the PowerShell Tools for Visual Studio 2017 installed for Visual Studio 2017.
I've only just begun organizing my scripts into projects and modules and I'm using VSTS integration in Visual Studio to handle the version control.
Reference Notes
I found that the book "Learn Windows PowerShell in a Month of Lunches" by Don Jones and Jeffrey Hicks an excellent guide for learning PowerShell. I also have watched some Pluralsight courses. I found the Beginning PowerShell Scripting for Developers very beneficial.