PowerShell – An introduction, Part II

This is a continuation of part I. If you haven’t read it I suggest at least going through the summary at the end of the post.

Working with Drives

In your basic command prompt you have the normal file system drives that you use to access your hard-drive, floppy, DVD, USB-stick, network shares etc. PowerShell uses this established interface to access other data sources as well. The following providers are accessible by default through drives in PowerShell 1.0:

  • Registry (HKLM: or HKCU:)
  • Certificate store (Cert:)
  • Environment variables (Env:)
  • Aliases (Alias:)
  • Functions (Function:)
  • Variables (Variable:)

When you install extensions for like the one fore IIS, etc. you usually get another drive accessible where you can look at the current webapplications and check their properties.


Selecting one of the PowerShell drives is easy. It is almost like in MS-DOS.


You can use the same commands as you’d do in MS-DOS, such as CD, DIR, MKDIR, etc.

To get a certain property for a registry key, (or a regular folder for that matter) you use the Get-ItemProperty cmdlet.

PS HKLM:\> CD system
PS HKLM:\system> CD CurrentControlSet\Control
PS HKLM:\system\CurrentControlSet\Control> Get-ItemProperty CrashControl
PSPath           : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control\CrashControl
PSParentPath     : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\system\CurrentControlSet\Control
PSChildName      : CrashControl
PSDrive          : HKLM
PSProvider       : Microsoft.PowerShell.Core\Registry
AutoReboot       : 1
CrashDumpEnabled : 2
Overwrite        : 1
LogEvent         : 1
DumpFile         : C:\Windows\MEMORY.DMP
MinidumpDir      : C:\Windows\Minidump

All in all it’s more or less the same working with Aliases or Environment variables. Note the following difference in output, though:

Name                           Value
—-                           —–
PS Env:\>

Did you note how putting a $-sign before the variable name gave us the value of the variable instead of a complete object? This brings us quite nicely to the subject of variables.


Variables aren’t that tricky to work with. They do not need to be pre-initiated. They do, however, require a $ to be identified as variables. Otherwise you get the following error message:

PS C:\> $Name = "Johan"
PS C:\> Name = "Johan"
The term ‘Name’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:5
+ Name  <<<< = "Johan"

PS C:\>

To display the value of a variable you simply type the name of the variable and PowerShell kindly returns the contents

PS C:\> $Name

When it comes to strings, you can choose for yourself if you want to use double quotes or single quotes, but there is one minor difference:

PS C:\> $str1 = "Hello"
PS C:\> $str2 = "World!"
PS C:\> $strCombo1 = "$str1 $str2"
PS C:\> $strCombo2 = ‘$str1 $str2’
PS C:\> $strCombo1
Hello World!
PS C:\> $strCombo2
$str1 $str2
PS C:\>

As you can see, a variable within double quotes will be evaluated, while within single quotes it will be interpreted literally.

Apart from strings you can also store integers and dates. Integers do not need delimiters and dates are most easily created using the Get-Date function

PS C:\> $birthday = 11/15/1973



Declaring Arrays is a breeze. PowerShell will automatically Split any comma-separated sequence of values so creating a arrays is as easy as this:

PS C:\> $Colors = "Red", "Green", "Blue"

The arrays are also dynamic by default, so adding values to them is a simple matter as well.

PS C:\> $Colors += "Cyan", "Magenta", "Yellow"

Creating two-dimensional arrays are done by declaring each row of the array as a one-dimensional array and then adding them to another array:

PS C:\> $arr1 = 1, 2, 3
PS C:\> $arr2 = "A", "B", "C"
PS C:\> $arr3 = $arr1, $arr2
PS C:\> $arr3[0][0]
PS C:\> $arr3[1][1]
PS C:\>


User input

Finally, to get dynamic user input you can use the Read-Host cmdlet.

$Name = Read-Host "What is your name?"



Okay, so to summarize we have now covered the following:

  • PowerShell uses system drives to access the registry, environment variables, etc.
  • Variables are preceded with a $
  • To access the value of a variable, simply type it’s name.
  • Arrays are comma-separated lists of variables
  • User input can be handled by the Read-Host cmdlet

Next post will probably cover some script logic such as if-clauses, looping, etc. After that I think it will be time to post some sample scripts.

/ Johan

Comments (2)

  1. Michael Mol says:

    We could use some PowerShell examples over at Rosetta Code, if you’re interested.  A good place to start would be in the <a href="http://www.rosettacode.org/wiki/Category:Control_Structures">control structures</a> tree.