SOLID Principles

   Submit to Reddit      

In a previous posting on behaviour-driven programming, I quickly referenced 'Uncle Bob' Robert C. Martin's website where he outlines the SOLID Principles.

I wanted to expand upon that and provide a few more helpful links.

The best examples I have seen that demonstrates these principles can be found in a series of short videos on

Scott Hanselman too has a number of podcasts where he is directly interviewing 'Uncle Bob': Hanselminutes Podcast 145 - SOLID Principles with Uncle Bob - Robert C. Martin

When I listened to this podcast, I was quite pleased to hear and have my personal experience validated when 'Uncle Bob' rated the Single Responsibility Principle as the most important. In my personal experience, and in the experience of a number of my colleagues, this principle alone has had the largest positive effect in the quest to write cleaner and more intuitive code.

Hanselminutes Podcast 150 - Uncle Bob Martin, this time with feeling

And a more recent podcast discussing the related topic of professionalism.

Hanselminutes Podcast 171 - The Return of Uncle Bob

Lessons In PowerShell - Variables and Declarations

   Submit to Reddit      

The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.

Automatic Variables

PowerShell includes the following in-built variables:

$$ Last token of the previous command line
$? Boolean status of last command
$^ First token of the previous command line
$_ Current pipeline object
$Args Arguments to a script or function
$Error Array of errors from previous commands
$Foreach Reference to the enumerator in a foreach loop
$Home The user's home directory; usually set to %HOMEDRIVE%\%HOMEPATH%
$Host Reference to the application hosting the POWERSHELL language
$Input Enumerator of objects piped to a script
$LastExitCode Exit code of last program or script
$Matches Hash table of matches found with the -match operator
$PSHome The installation location of Windows PowerShell
$profile The standard profile (may not be present)
$StackTrace Last exception caught by Windows PowerShell
$Switch Enumerator in a switch statement
$True Boolean True
$False Boolean False
$Null Null

Variable Declaration

Variables and other data elements may be instantiated in different scopes:

  • Variables in the global scope are visible in all scopes.
  • Variables in the script scope are visible to all scopes within that script file.
  • Variables in the local scope are visible only in the current scope and its children.
  • Private scope variables are visible only to that current scope.

A scope is created in the body of a shell function.


$[scope:]name or ${anyname} or ${any path}


$a = 1
$global:a = 1    # Visible everywhere
$local:a = 1    # defined in this scope and visible to children
$private:a=1    # same as local but invisible to child scopes
$script:a=1    # visible to everything in this script
$env:path = "d:\windows"
${C:\TEMP\testfile.txt}="This writes to a file"
Get-Variable -scope 1 a    #Gets value from the parent scope
Get-Variable -scope 2 a    # grandparent

Type Declaration

Variables also can be declared as specific data type by prefixing the variable declaration with the data type.

[bool] or [boolean] A boolean (True or False) value
[byte] An 8-bit unsigned character
[char] A Unicode 16-bit character
[string] String of Unicode characters
[datetime] A System.DateTime object
[int] A 32-bit signed integer
[long] A 64-bit signed integer
[single] A Single-precision 32-bit floating point number
[double] Double-precision floating number
[decimal] A 128-bit decimal value
[xml] A xml object
[array] An array of values
[hashtable] A System.Collections.Hashtable object
[wmi] Windows Management Instrumentation (WMI) instance or collection
[wmiclass] WMI class
[adsi] Active Directory Services object

A variable's type also can be declared as a .NET Framework class by using the full class name. For example:

[System.Int32] $amount = 1234

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!