Thursday, May 29, 2008

Select and then Sort


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


PS > (measure-Command { 1..10 | foreach { gsv | select name,status | sort name}}).TotalMilliseconds


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


PS > (measure-command { 1..10 | foreach { gps | select -last 5 | sort WS }}).TotalMilliseconds

: 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
PS > (measure-Command { 1..10 | foreach { dir *.ps1 | select LastWriteTime, CreationTime | sort @{Expression={$_.LastWr
iteTime-$_.CreationTime}; Ascending=$false}}}).TotalMilliseconds


 Result: Second command is 8.2% faster


Test #3  

PS > (measure-Command { 1..10 | foreach { dir | sort -unique | select name}}).TotalMilliseconds


PS > (measure-Command { 1..10 | foreach { dir | select name | sort -unique}}).TotalMilliseconds


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!.

Wednesday, May 28, 2008

Generating Random Dates


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


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)

PS > Get-Random ([datetime]::maxValue.ticks)

Yes I can! So I tried to generate a new DateTime using that method:


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.

Adobe Illustrator to XAML converter


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.

Friday, May 23, 2008

Fine Grain Password Policy Tool (cmdlet)

In Microsoft Windows 2000 and Windows Server 2003 Active Directory domains, only one password policy and account lockout policy could be applied to all users in the domain.
The Windows Server 2008 operating system provides a way to define different (fine-grained)password and account lockout policies and apply different restrictions to different sets of users in a domain.

Christoffer Andersson, Directory Services MVP, has released the second beta of FGPP - Fine Grain Password Policy Tool. The tool is available as MMC snap-in and a PowerShell cmdlet.

A quick start guide, download link and usage examples are available on Christoffer's blog.

Thursday, May 22, 2008

PowerShell Community Toolbar


The PowerShell Community Toolbar has passed 200 installations. Don't know what I'm talking about? Find out here.

Tuesday, May 20, 2008

Explore your mobile device with Windows PowerShell


Oisin Grehan [MVP] has released a beta version of his PowerShell provider for mobile devices: SmartPhone, PocketPC and Windows Mobile. Now you can start browsing your mobile like any other PowerShell drive:

ps c:\> add-pssnapin psmobile
ps c:\> cd mobile:
ps mobile:\> cd \temp
ps mobile:\temp> copy \windows\*.gif .
ps mobile:\temp> ii banner.gif
ps mobile:\temp> ii -local banner.gif
ps mobile:\temp> ii \windows\tasks.exe
ps mobile:\temp> convertfrom-wmfile *.gif c:\temp\


This is COOL!

Get all the details and download it at CodePlex.

Monday, May 19, 2008

PowerScripting Podcast - Episode 26


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.

CoreConfigurator updates


I've blogged about this cool utility before,  and now it got updated with more goodies. The latest version of CoreConfigurator support the option to enable/disable Automatic Updates and configure Backup performance settings. You can download it here.



There is also a two part screen cast, by Dean Stefanov (from Check it out.

Sunday, May 18, 2008

PowerShell on Server core


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.

The Digital Photography and Imaging Show


ShutterSpeed is a new series of Digital Photography and Imaging available on Channel 10.

Each episode of ShutterSpeed we'll help you understand your camera gear so that you can take better photos and show you new and cool ways to edit, share and display your images.

The first episode discusses online video storage, photo editing tools, the principles of photography basics and what to look for in a digital camera.

Saturday, May 17, 2008

Windows PowerShell Pocket Reference


Check it out, one more book to your PowerShell book shelf.



This portable reference to Windows PowerShell summarizes both the command shell and scripting language, and provides a concise reference to the major tasks that make PowerShell so successful. It's an ideal on-the-job tool for Windows administrators who don't have time to plow through huge books or search online. Written by Microsoft PowerShell team member Lee Holmes, and excerpted from his Windows PowerShell Cookbook, Windows PowerShell Pocket Reference offers up-to-date coverage of PowerShell's 1.0 release. You'll find information on .NET classes and legacy management tools that you need to manage your system, along with chapters on how to write scripts, manage errors, format output, and much more.

Beginning with a whirlwind tour of Windows PowerShell, this convenient guide covers:

  • PowerShell language and environment
  • Regular expression reference
  • PowerShell automatic variables
  • Standard PowerShell verbs
  • Selected .NET classes and their uses
  • WMI reference
  • Selected COM objects and their uses
  • .NET string formatting
  • .NET datetime formatting

An authoritative source of information about PowerShell since its earliest betas, Lee Holmes' vast experience lets him incorporate both the "how" and the "why" into the book's discussions. His relationship with the PowerShell and administration community -- through newsgroups, mailing lists, and his informative blog Lee Holmes -- gives him insight into problems faced by administrators and PowerShell users alike. If you're ready to learn this powerful tool without having to break stride in your routine, this is the book you want.

Sunday, May 11, 2008

PowerShell CTP2 on Server 2008

Updated: 05/14/2008 ( See original post below)


It won't work! (adding the command-line backup-tools when CTP(2) is installed) At the end of the process I checked the $host variable (thanks Marco) and it  showed that v1 is the current version. I jumped into conclusions too early and got fooled by PowerShell console title :(

Installing the backup-tools via serverManagerCmd (like the MMC) installs ALL backup dependencies (PowerShell v1 and all backup-features), it removes CTP without any confirmation. However, PowerShell CTP2 still appears under Control Panel > 'Programs and features' and the CTP2 program shortcuts are still available in the Programs menu but it points to v1 (installed by serverManager).

The confusion stems from the console title which shows: 'Windows PowerShell V2 (CTP2)' as it appears in the shortcut. I should have check the $host output or make sure that the CTP text logo appears in the console, CTP versions logo says:

Windows PowerShell V2 (Community Technology Preview - Features Subject to Change)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.

So, my apologies for misleading you! I'm still researching the issue and hope to find a solution (if possible).






Original Post:

There was a discussion at the PowerShell newsgroup regarding the inability to add the Backup-Tools feature when PowerShell CTP is installed on Server 2008. I was already installing Server 2008 on VM, so I decided to give it a try. I had no problems whatsoever and the installation was successfull. Here is a video capture of the process. The capture includes the following steps:

Server 2008 is already installed (default install)

1. Install PowerShell CTP2 on Server 2008.
2. Adding the Backup-Tools server component (registers the Windows.ServerBackup PowerShell snap-in)
3. Reboot the Server.
4. Loading the ServerBackup snap-in and listing its cmdlets.


 PowerShell CTP2 on Server 2008

PowerShell CTP2 on Server 2008

Saturday, May 10, 2008

Turn on PowerShell in Server 2008


PowerShell v1 is included with Windows Server 2008 as a feature/optional component and it is turned off by default. To use it you have to add it using Server Manager, which available as: MMC console and a command line utility.

Here is how to turn it on, only one line of code, using the ServerManagerCmd.exe command-line tool. BTW, ServerManagerCmd also has a parameter -whatIf, I wonder where did it came from :)

-i Is a shortcut for -install
-w is for -whatIf


When the installation process ends, PowerShell's shortcuts are available under the Start menu:


Launch PowerShell (no need to reboot) and you're ready to go, don't forget to change the execution policy if you need to execute scripts.




More Info:

Saturday, May 3, 2008

Windows PowerShell V2 - Not a beta!


At last, It's out! The Windows PowerShell Team has released the second Community Technology Preview (CTP2) of Windows PowerShell V2. Read the announcement.

To see what's new in this release download the attached slides of this post MMS: What's Coming In PowerShell V2.


Download Windows PowerShell V2 CTP2

Thursday, May 1, 2008

AD cmdlets - new public beta


Quest AD cmdlets 1.1.0 public beta is available for download. It includes bug fixes and one new feature: The Get-QADGroupMember has a new parameter -Indirect. This parameter, when specified expands nested group objects in addition to objects that are direct members of the group.