Subscribe / Unsubscribe Enewsletters | Login | Register

Pencil Banner

The power of PowerShell: Essential tips Windows admins will love

Adam Bertram | Nov. 6, 2015
Make the most of Microsoft’s command line by mastering the nuances of the PowerShell language.

One of the oldest ways to create a custom object that works on all versions of PowerShell is via the New-Object cmdlet. To create a blank custom object of type System.Management.Automation.PSCustomObject with no properties using New-Object you'd simply call New-Object and specify the TypeName parameter of PSObject.

$object = New-Object –TypeName PSObject

$object = New-Object –TypeName PSObject

This doesn't do us any good, however, because it contains no properties. To add properties, we can use the Add-Member cmdlet. This cmdlet essentially binds a new member (or property) to an existing object similar to the one we created.

$object | Add-Member –MemberType NoteProperty –Name MyProperty –Value SomeValue

$object | Add-Member –MemberType NoteProperty –Name MyProperty –Value SomeValue
Click on image to enlarge.

You can see that Add-Member has a MemberType parameter. As I mentioned earlier, when creating your own objects you will typically use the NoteProperty type here. At this point, you simply need to specify the name of the property and the value that the property will hold.

From here, you can repeat adding as many properties as you'd like using Add-Member.

Next, rather than using Add-Member you can specify all your properties up front in a hashtable and pass those properties to New-Object.

$properties = @{ 'MyProperty1' = 'Value1'; 'MyProperty2' = 'Value2' }
$object = New-Object –TypeName PSObject –Property $properties

$object = New-Object –TypeName PSObject –Property $properties
Click on image to enlarge.

Finally, as of PowerShell v3, we can use the [pscustomobject] type accelerator. Occasionally, the PowerShell team will create what's called type accelerators. These are convenient shortcuts to create objects of particular types. Since creating custom types are so common in PowerShell, they decided to create one for creating them. Nowadays, this is the most common way to create custom objects.

To create custom objects with the [pscustomobject] type accelerator you'll first need to create a hashtable with property names as key names and their values as the hashtable values. Let's reuse the hashtable we created earlier.

$properties = @{ 'MyProperty1' = 'Value1'; 'MyProperty2' = 'Value2' }

Now, instead of using the New-Object cmdlet and specifying the type and properties we can simply cast that hashtable directly to a custom object simply by "declaring" the hashtable as a custom object type.

[pscustomobject]$properties

 [pscustomobject]$properties
Click on image to enlarge.

You'll see it’s a much faster way to create a custom object. I recommend this approach when working with PowerShell v3 or later. It's, by far, the easiest to remember and is the most readable.

 

Previous Page  1  2  3  4  5  Next Page 

Sign up for CIO Asia eNewsletters.