Dmitry Nikolaev - stock.adobe.co
Learn PowerShell scripting using the PSKoans module
Want to get started with PowerShell automation to manage your Windows systems? Try the PSKoans module to learn as you go through its exercises to build your scripting skills.
Administrators in Windows shops who want to future-proof their jobs should look at automation -- particularly PowerShell -- to make their management efforts more efficient.
Administrators who want to learn PowerShell scripting will often find it difficult to get started when faced with a blinking cursor on the command line. How does one progress from simple one-liners to managing users and resources both inside the data center and the Azure cloud? There are multiple ways to build this proficiency, but PSKoans is an interesting use of PowerShell and Pester as a learning tool. Though still in its infancy, I can see it developing into a very useful learning tool that will complement rather than replace other learning options.
There are several methods to learn PowerShell scripting, including formal classes, online tutorials, video tutorials, books, and trial and error. My personal preference is a mixture of a good book, written by an established expert on the subject, and experimentation. Your choice may well be different. Whatever method you adopt, make sure it's one you're comfortable with and actually works for you.
If you want, or need, to learn PowerShell scripting, you have all of the above options available to you. There is also another option -- the PSKoans module -- which is a somewhat unique PowerShell learning resource.
What is the PSKoans module?
A koan is a story, dialog, question or statement used to test a student's progress. PSKoans is a PowerShell module authored on GitHub by Joel Swallow that presents the user with a PowerShell challenge they must solve to move on to the next problem.
The module is the PowerShell Gallery. Prerequisites for using PSKoans are:
- Windows PowerShell v5.1 or PowerShell Core 6 (or above);
- NuGet package provider (you will get a prompt to install if necessary); and
- Pester PowerShell module, which is usually used to test code but also used to test infrastructure and troubleshooting infrastructure problems.
Instructions to install PSKoans are on the module's GitHub site. As part of the installation, I updated my Pester instance to version 4.9.0. I ran PSKoans in PowerShell v7 preview 5 for this article.
Installing the module from the PowerShell Gallery looks like this:
Install-Module -Name pskoans
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y
If you don't have the NuGet package provider installed, the system will prompt you during the download process. Once the module is installed, run:
Show-Karma
The results appear in Figure 1.
The Show-Karma output shows:
× It is a simple comparison
at <ScriptBlock>, C:\Users\Richard\PSKoans\Introduction\AboutAssertions.Koans.ps1: line 27
27: $____ | Should -Be $true
The message indicates the user needs to edit the AboutAssertions.Koans.ps1 file to correct the errors.
To open all PSKoans files in VSCode, run the following command:
Show-Karma -Meditate
Next, navigate to the required file as shown in Figure 2.
Line 27 displays as follows:
$____ | Should -Be $true
However, it should be adjusted to the following:
$true | Should -Be $true
Save the file and rerun Show-Karma as illustrated in Figure 3.
You need to repeat the process of working through the assertions in the file to fix all the issues. You can work through each question individually or answer them all in one go.
Once you've dealt with the four issues in the AboutAssertions file, you automatically move onto the next file in the sequence: AboutBooleans.
What are the pros and cons of PSKoans?
I haven't worked through every koan file in PSKoans, but I had the following observations.
The explanation of why a specific answer is correct, or even the underlying knowledge to determine the correct answer, is not always present in the koan files. For example, AboutStrings doesn't explain why string expansion only occurs when using double quotes instead of single quotes.
By default, you have to work through the topics in the order that PSKoans dictates. You can view the list of available topics with the following command:
Show-Karma -List
Topic Module Position
----- ------ --------
AboutAssertions _powershell 101
AboutBooleans _powershell 102
AboutNumbers _powershell 103
AboutStrings _powershell 104
AboutCmdletVerbs _powershell 105
<truncated for brevity>
AboutEnumerations _powershell 310
AboutClasses _powershell 311
AboutRegularExpressions _powershell 320
AboutOutCmdlets _powershell 401
AboutCsvCmdlets _powershell 402
The Get-PSKoan command provides the same information.
You can view your progress in individual topics by following the example below:
Show-Karma -Topic AboutArrays
You could work on the AboutArrays.Koans.ps1 file and keep using the Topic parameter if you want to look at specific areas of PowerShell instead of following the default path. This is probably more useful if you have some experience with PowerShell and want to delve into more advanced areas.
One useful feature that I didn't have chance to test was the Update-PSKoan function. According to the help file, the function will "[u]pdate the user Koan directory with new topics. Topics will be moved to new directories if appropriate. Old files will be removed. Existing koan topics are updated with new koans. Answers are preserved." This is a very good option, as PSKoans is still a work in progress.
The Show-Advice command displays random PowerShell advice. But it would also be beneficial to find help in areas the user could specify. Hopefully, this will be rectified in subsequent releases.
Who is PSKoans good for?
Is PSKoans a good learning tool for PowerShell? My answer, after the limited testing I've performed, is yes and no.
If you're setting out to learn PowerShell from scratch, I wouldn't recommend PSKoans as your only method. There isn't enough background information in the koans files for them to work as your primary learning tool. A formal class or a book providing a structured tutorial. PowerShell's GitHub site supplies a list of learning resources. The book Why Learn PowerShell by Ed Wilson. These are good starting points.
I see PSKoans being very useful in a number of scenarios:
- You have some PowerShell knowledge that you want to be tested on.
- You're working through a structured tutorial and want more practical experience.
- You're mentoring a colleague who is learning PowerShell and use PSKoans as a structured set of tests to measure progress.
I could even see PSKoans used in the interview process in which the candidate must complete the questions in the more advanced portions of the koan files.