Prof. Powershell

Splat! Goes the New Object

New-Object's Property parameter can bust out a hashtable of property names and values that's easier on the eyes.

If you've seen any of my PowerShell scripts from either my Mr. Roboto column or my blog, you'll see that I use custom objects quite a bit. I typically create a custom object and write it to the pipeline so I can use it with other PowerShell expressions. Here's how I might have done this:

PS C:\> $obj=new-object psobject
PS C:\> $obj | add-member noteproperty name Jeff
PS C:\> $obj | add-member noteproperty size 123
PS C:\> $obj | add-member noteproperty description "Prof. PowerShell"
PS C:\> $obj | add-member noteproperty OS (gwmi win32_operatingsystem).caption
PS C:\> $obj | fl

name        : Jeff
size        : 123
OS          : Microsoft Windows 7 Ultimate
description : Prof. PowerShell

There's nothing necessarily wrong with this approach. It works and is relatively easy to follow. But PowerShell 2.0's version of the New-Object cmdlet has a parameter called Property that takes a hashtable of property names and values. This is referred to as a splat:

PS C:\> $obj=new-object PSObject -Property @{name="Jeff";computer=$env:computername}
PS C:\> $obj

name                computer
----                --------
Jeff                GODOT7

The hash table, also known as a dictionary or associative array, is a semi-colon delimited of name=value pairs. For an object with a few properties, I can do this on a single line. For objects with more properties I suggest using this format:

$obj=new-object PSObject -Property @{
  name="Jeff";
  size=123;
  description="Prof. PowerShell";
  OS=(gwmi win32_operatingsystem).caption
}

This creates the same object I originally created at the beginning of the lesson, but this is a little easier to read, especially if the new property value is returned from a more complex PowerShell expression. For the majority of your PowerShell scripts, this approach should be all that you need. If you need additional properties after you initially create the object, then you can use Add-Member:

PS C:\> $obj | Add-Member Noteproperty PSProfile $profile
PS C:\> $obj.psprofile
C:\Users\Jeff\Documents\WindowsPowerShell\
  Microsoft.PowerShell_profile.ps1
PS C:\> $obj

OS          : Microsoft Windows 7 Ultimate
name        : Jeff
description : Prof. PowerShell
size        : 123
PSProfile   : C:\Users\Jeff\Documents\WindowsPowerShell\Micro...

Finally, if you want to create script methods for your custom objects, you'll still need to use Add-Member. However I expect that for many of you, splatting your way to new, custom objects will be more than enough to keep you happy and busy.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular