Creating New Objects

by Dec 9, 2009

There is a powerful new way in PowerShell v.2 to create new objects from scratch: You should first get yourself an empty hash table object, then add all the information to the hash table and finally use New-Object with its new -Property parameter to turn the hash table into an object.

$hash = @{}
$hash.Firstname = "Tobias"
$hash.Lastname = "Weltner"
$hash.Age = 99
$object = New-Object PSObject -Property $hash
$object

Your new object can then be used to consolidate information from different sources, or can serve as input object to cmdlets that accept multiple inputs to ValueByPropertyName.

Twitter This Tip! ReTweet this Tip!