When you use both select-object and sort-object in a pipeline, what's the proper order? Let's check how fast they execute.
Each speed test is built from two similar commands with a different sort/select piping order and each test is executed 10 times. The total execution time is measured in Milliseconds.
- Updated: 06/11/2008 (see comment below by Lee Holmes) -
Test #1
PS > (measure-Command { 1..10 | foreach { gsv | sort name | select name,status }}).TotalMilliseconds
444.5451
PS > (measure-Command { 1..10 | foreach { gsv | select name,status | sort name}}).TotalMilliseconds
384.7565
Result: Second command is 15% faster.
Test #2
# this command is the third example of select-object command from the help files.
PS > (measure-command { 1..10 | foreach { gps | sort ws | select -last 5 }}).TotalMilliseconds
346.0652
PS > (measure-command { 1..10 | foreach { gps | select -last 5 | sort WS }}).TotalMilliseconds
100.4444
Result: Second command is 3.44 times faster.
# this command is the sixth example of sort-object command from the CTP help files. I changed the extension to ps1.
PS > (measure-Command { 1..10 | foreach { dir *.ps1 | sort @{Expression={$_.LastWriteTime-$_.CreationTime}; Ascending=$
false} | select LastWriteTime, CreationTime}}).TotalMilliseconds
640.9769PS > (measure-Command { 1..10 | foreach { dir *.ps1 | select LastWriteTime, CreationTime | sort @{Expression={$_.LastWr
iteTime-$_.CreationTime}; Ascending=$false}}}).TotalMilliseconds
592.3512
Result: Second command is 8.2% faster
Test #3
PS > (measure-Command { 1..10 | foreach { dir | sort -unique | select name}}).TotalMilliseconds
6405.653
PS > (measure-Command { 1..10 | foreach { dir | select name | sort -unique}}).TotalMilliseconds
750.8251
Result: Second command is 753% faster!
The reason why 'select then sort' is faster to execute is because there are much less properties for sort-object to work on. When you select certain properties from a collection, select-object creates a new object with just the specified properties of the incoming object thus resulting in a smaller object to process.
One thing is for sure: In most cases, select objects before sorting them, and ALWAYS make sure they produce the SAME output!.
One of the new cmdlets in PowerShell CTP2 is Get-Random. Get-Random gets a random number or selects objects randomly from a collection.
I wanted to generate random DateTime objects with it but there is no built-in capability for that, yet it doesn't mean you can't use Get-Random it to generate random dates :)
At its base, a DateTime object is measured in 100-nanosecond units called Ticks. So I tried to use Get-Date to initialize a DateTime using Ticks. Again, Get-Date doesn't have any parameter for that so I checked the underlying object, System.DateTime.
One of the System.DateTime constructors allows you to initialize a new instance of the DateTime structure to a specified number of ticks. The maximum value for ticks is determined by:
PS > [datetime]::maxValue.ticks
3155378975999999999
Now, Can I use Get-Random to generate a number in the range of 0-3155378975999999999 (exclusive)?
The help for Get-Random says: Without parameters or input, "Get-Random" returns a randomly selected 32-bit unsigned integer between 0 and Int32.MaxValue (0x7FFFFFFF, 2,147,483,647), so it shouldn't be a problem with the ticks input to generate a higher value then the max value of Int32.MaxValue:
PS > Get-Random ([datetime]::maxValue.ticks)
1.34540044929813E+18
PS > Get-Random ([datetime]::maxValue.ticks)
8.71609536968019E+17
Yes I can! So I tried to generate a new DateTime using that method:
PS > [datetime]::maxValue.ticks
3155378975999999999
PS > new-object dateTime (Get-Random ([datetime]::maxValue.ticks))
Friday, March 09, 3951 11:00:06 PM
PS > new-object dateTime (Get-Random ([datetime]::maxValue.ticks))
Wednesday, October 07, 2476 1:36:05 AM
Cool. But this can give a date between the Year 0 and 9999:
PS > new-object datetime ([datetime]::maxValue.ticks)
Friday, December 31, 9999 11:59:59 PM
If you want to have random DateTime between a specific period of time then you can use the Get-Random -min and -max parameters:
PS > $dateMin = get-date -year 1980 -month 1 -day 1
PS > $dateMax = get-date -year 2007 -month 1 -day 1
PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks)
Thursday, November 14, 2002 9:45:23 AM
PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks)
Friday, December 07, 2001 6:17:54 PM
PS > new-object datetime (Get-Random -min $dateMin.ticks -max $dateMax.ticks)
Monday, April 01, 2002 4:50:52 PM
I didn't find any use for random dates yet, but it is good to know you can create them.
AI>XAML is plug-in, by Michael Swanson, that enables graphic designers to export vector artwork from Adobe® Illustrator® to WPF and Silverlight compatible XAML. It's free for download and there is also a video tutorial on Channel9.
I need to try it in WPF forms! This week, the PowerShell blog sphere is full with WPF walk through's introduced by Joel, MoW and James Brundage from the PowerShell team.
The PowerShell Community Toolbar has passed 200 installations. Don't know what I'm talking about? Find out here.

PowerScripting Podcast - Episode 26 is out, this time with the legendary Keith Hill, Windows PowerShell MVP. You can also listen to the podacst online via PowerShell Community Toolbar.
And for a limited time, Quest, the sponsor of the PowerScripting Podcast is offering Powerscripting Podcast listeners a free copy of Jeffery Hicks [MVP] upcoming book, 'Managing Active Directory with Windows PowerShell: TFM' from Sapien Press. Get the full details at the PowerScripting Podcast website.
Impossible??? Not anymore! Check Dmitry's blog for a step-by-step guide on installing Windows PowerShell on Windows Server 2008 in Server Core mode.