Lessons In PowerShell - Documentation

   Submit to Reddit      

When you install PowerShell, documentation is also installed. This documentation can be found under the following folder:

Start Menu ➞ All Programs ➞ Windows PowerShell 1.0 ➞ Documents ➞ en-US

The Windows PowerShell Getting Started Guide (GettingStarted.rtf) is a 32-page introduction to using the shell and the Windows PowerShell scripting language.

The Windows PowerShell Primer (UserGuide.rtf) is a companion document to the Getting Started Guide. It describes the features of Windows PowerShell and the Windows PowerShell scripting language with examples of how to use the new shell.

The Windows PowerShell Language Quick Reference (QuadFold.rtf) is a one-page foldable guide to the Windows PowerShell scripting language.

Lessons In Windows PowerShell - Hello World

   Submit to Reddit      

A "Hello World" script is probably a good starting point.

Start PowerShell from Start Menu ➞ All Programs ➞ Windows PowerShell 1.0 ➞ Windows PowerShell.

At the PowerShell command prompt, the command "Help" shows the various commands and aliases that are available in PowerShell.

The command "Write-Host Hello World" will essentially do the same as the old Command Prompt "echo" command - which is great for our purpose here. But that is too easy... we want a script!

In a text editor, create a text file named "test.ps1" and save it to the current path within PowerShell. Add the following text to the file and save it.

Write-Host Hello World

Back in the PowerShell window, how do we run the script?

Just typing the "test.ps1" produces the following error.

The term 'test.ps1' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:8      
+ test.ps1 <<<<

Instead, the name of the script needs to be prefixed with a period followed by a forward or backwards slash.

Run the command "./test.ps1".

Since we haven't digitally signed the script, another error appears.

File test.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:10      
+ ./test.ps1 <<<<

This error occurs because by default the execution policy that affects all users on a machine is set (in the Windows Registry) such that unsigned scripts are disabled.

The command "Get-ExecutionPolicy" shows the current policy about executing scripts. Currently it is set to "Restricted".

The command "Get-Help Set-ExecutionPolicy" provides general information about the Set-ExecutionPolicy command.

The command "Get-Help Set-ExecutionPolicy -detailed" provides more detailed information about the Set-ExecutionPolicy command.

The command "Get-Help Set-ExecutionPolicy -full" provides the most information about the Set-ExecutionPolicy command.

The command "Set-ExecutionPolicy -executionPolicy RemoteSigned" will set the policy such that only scripts that are downloaded from the internet are required to be signed by a trusted publisher.

However, on Windows Vista the following error occurs because it requires escalated privileges.

Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1ShellIds\Microsoft     
.PowerShell' is denied.      
At line:1 char:20      
+ Set-ExecutionPolicy <<<< -executionPolicy RemoteSigned

The easiest way to change the execution policy in Vista is to start PowerShell from the start menu by right-clicking and selecting "Run as Administrator". From within that PowerShell the Set-ExecutionPolicy command will work. Close that window after executing the command.

Finally we can then successfully execute the script with the command "./test.ps1", and it actually works!

Lessons In Windows PowerShell - Introduction

   Submit to Reddit      

One of our Team Foundation Server build servers recently ran out of disk space. Upon closer investigation, we had 6 team projects building on that server, each with at least 3 different code branches, totalling more than 18 build folders - with each build folder larger than 2Gb. Do the maths, and that is essentially more than 40Gb being used.

Those folders however are not needed after a build has completed, and they can be safely deleted - so I decided that I would write a Windows PowerShell script to delete folders that haven't been written to for more than a day, and schedule the script to run through the Windows Task Scheduler.

Well, there was only one problem... I haven't really used PowerShell before. But, a couple of hours later, and I had the solution.

The start of this series of lessons reflects some of the lessons I quickly learned.

You can find more information on Windows PowerShell at the following links.

Towards Behaviour-Driven Programming - Part 2

   Submit to Reddit      

Today a small team of three had a post-iteration meeting, and for the first time within the company (I dare say), there was agreement about the benefits of BDD/TDD when automated tests are required. The team agreed that it takes significantly longer to retrofit automated tests than it does to do the coding and testing in a BDD/TDD fashion.

The main reason is because when BDD/TDD practices are followed, the code is designed cleanly from the start and it tends to naturally follow best practices. When tests were retrofitted, the code typically has to be refactored/restructured and sometimes even completely rewritten so that the test cases can be properly identified and then written.

This is a hard-earned and yet relatively small victory, and after a long amount of time, suffering and battling, I'm telling everyone who'll listen!

One step closer... critical mass... you are now in my sights!