Monthly Archive


Monthly Archives: March 2009


Working on a script to compare two lists of computer names (plus some other stuff) I wasn’t happy with the performance I was getting from my first try.  I started to experiment but needed to measure the time the scripts took.  Enter Measure-Command.

Measure-Comand {./script.ps1}

Output is a timespan object that gives the total execution time of the script block.  It doesn’t have to be a full script – any valid script block will work. Normally I don’t worry too much about performance but in this case it just felt the approach was too slow.

Keep Measure-Command in mind for during your testing


Technorati Tags: PowerShell,CTP3

PowerShell uses

Normally we think of using PowerShell to perform admin tasks - creating users in AD etc - using cmdlets or scripts.  I have found myself using it for other tasks - searching through dumps of ipconfig listsing to get IP addresses; matching two lists of computer names to find discrepancies
PowerShell is a powerful tool with many uses - the limit is our imagination


One thing I didn't really take much notice of was the difference is spelling between English english and US english.  I have read so many technology and science books (nearly all published in the USA) that I just accepted the differences.
Until I come to write against a template based on US English where anything ending -ise is flagged as an error 'cos the dictionary thinks it should be -ize.  You don't realise how many times you use words like that until somethign like this comes along
The Victorians who introduced using -ise instaed of -ize have a lot to answer for.

Computer cmdlets

I was digging into some aspects of Exchange 2007 yesterday for Chapter 12 of my book (Chapter 11 is finished and will be on MEAP soon)  and needed to restart some of the VMs I was using.  I remembered the restart-computer that comes with V2.  It saved some time compared to logging into the offending machine and then going through the restart procedure.
Simple syntax
Restart-Computer -ComputerName target -Force
Force takes care of the situation where users are logged on.  It can also be run as a job.
Stop-Computer is a bit more permanent  🙂


If you use PowerShell and SQL Server you should at the very least look at SQLPSX which is a big library of PowerShell functionality.  Version 1.5 has just been released.  See!EA42395138308430!315.entry for details


Technorati Tags: PowerShell,SQL Server

Test folders

I needed some test folders in a hurry.  They didn’t have to have anything in them – just exist as a set of folders for me to work with.

From the PowerShell prompt

1..20 | foreach {md "test$_"}

How easy is that.  Twenty folders in the time it would take me to open Windows explorer. 

Use the range operator to define a range of suffixes and pipe into foreach.  The md command creates the folder using string substitution to append the suffix.

If you want to be slightly more formal about things then the Sunday best version of the command is

21..40 | foreach { New-Item -Name "test$_" -ItemType directory }

The range operator is a very useful tool that probably doesn’t get as much exposure as it should


Technorati Tags: PowerShell

Active Directory Cookbook third edition

The original AD cookbook by Robbie Allen was published back in 2003. It has been one of my favourite, and most referenced, books.  When I used to use VBScript (shudder) it was indispensible and saved me vast amounts of time.  One AD implementation I did we created the whole AD structure – OUs, Sites, Subnets, Links etc form a single script! Even after moving to PowerShell I found the book very useful as I could translate the scripts in the book into PowerShell.

The third edition has just been published – ISBN 9780596521103. Like the second edition it is co-authored by Laura E Hunter and Robbie Allen. My first impression is that it is a huge book with 1015 pages of fairly small type. It has definitely grown from the first edition. 21 chapters supply nearly 500 recipes to solve AD administration problems. This edition has expanded a bit to cover ADAM\ADLS, ADFS, Exchange and ILM as well as AD itself.

Most of the recipes will present a problem and show how to solve it using the GUI tools, command line utilities, VBScript and PowerShell. Not all possibilities are available for all recipes.

Having been very impressed with the original I was delighted to see that the high quality has been maintained. The descriptions are concise but cover the required information.

The PowerShell sections feel like a last minute addition in places and could do with being a bit more consistent. In places the solutions jump from scripts to the Quest AD cmdlets and back again with no explanation.  The explanatory chapter at the front could do with more PowerShell content. I think a beginner would find getting into the PowerShell scripts harder than the VBScripts.  With Windows 2008 R2 including AD cmdlets I think the next edition would be the time to adopt a consistent approach to the PowerShell content and make it the primary scripting tool in the book.

Even with those comments this is an excellent book that I would have no hesitation in recommending. If you have the 1st edition buy it – its worth the upgrade. If you don’t have a copy buy it. Just buy it – if you do any AD admin work the book will pay for itself in the time you save.

PowerShell v1.0 is available on Microsoft Update

Microsoft have added PowerShell v1.0 as an optional update that is available through the update mechanisms.

PowerShell 1.0 through MU is only offered:

  • As an optional update
  • If you don’t have any other version of PowerShell installed (including V2 CTPs) on your machine
  • .NET 2.0 is installed on the machine
  • OS is one of: XP-SP3, W2K3-SP2, Vista-RTM, Vista-SP1

Hopefully v2 will be available through the same mechanism.

This will make deployment of PowerShell through out the enterprise a much easier proposition

Technorati Tags: PowerShell,Deployment

Quest AD cmdlets

If you have followed this blog over any period of time you will know that I am a big fan of the Quest AD cmdlets.  The good news is that version 1.2 of the cmlets has gone RTM.  The bad news is that it won’t be available for a little while yet.  The major details of the changes are available from

The ability to restore tombstoned objects is very useful as is the ability to directly pull group memberships from an object. 

There are a number of other smaller changes that I will cover in due course.

If you work with PowerShell & AD keep your eyes open for the announcement of their availability

Its all in context

I spent part of this afternoon using select-string to track down where particular settings occur in GPOs.  Use the SDM GPMC cmdlets to dump the settings to xml and then use select-string on the xml.  So much easier than wading through the GUI.

In PowerShell v1 select-string returned the line containing the text we are searching for

PS> Select-String *.txt -Pattern "target text"

file1.txt:9:some text with what we want - - - target text
file2.txt:15:some text with what we want - - - target text

In v2 we can get the context of the target ie lines before and after

PS> Select-String *.txt -Pattern "target text"   -Context 0,1

> file1.txt:9:some text with what we want - - - target text
  file1.txt:10:and lest we forget -- we want the context as well
> file2.txt:15:some text with what we want - - - target text
  file2.txt:16:and lest we forget -- we want the context as well

Note the > to show the line with the actual text.  Context is specified as number of lines before, lines after the target.

This is fine but what if you want to actually separate out the results.  At the moment we get file name, line number and the line.  what would be good would be data separated out.  First go is

Select-String *.txt -Pattern "target text"   -Context 0,1 | Select FileName, Line, Context

This doesn’t work as the object type is returned for the context.  We can use get-member to dig into the context and then do something like this

Select-String *.txt -Pattern "target text"   -Context 0,1 | foreach {
"{0} {1} {2} {3} " -f $_.FileName, $_.LineNumber, $_.Line, $($_.Context.PostContext)


The context collection has both a Post context and a Precontext member.   Once we have the data we can format it at leisure.

The context could be used in a number of places eg picking DNS servers out of ipconfig files.

Select-string is a very useful cmdlet and one that it pays to get to know.  BTW the pattern is actually converted to a regular expression!

Technorati Tags: PowerShell,V2