You can use the New-Object cmdlet to generate an object of any type. The two choices for custom objects are PSObject and Object PSObject creates an object of class System.Management.Automation.PSCustomObject Object creates an object of class System.Object While PSObject requires a bit more overhead, it is generally preferred. The drawbacks to Object are explained in this article: http://cjoprey.wordpress.com/archived/custom-object-gotchas/ You can also specify -ComObject to create a COM Object, but that is not covered in this article. Example:
#Example 1.1
$obj =
new
-
object
psobject
There are two different ways to add members to an object
The best (and most verbose) method is to use Add-Member. The reason for this is that you can specify the type of member to add (all other methods assume NoteProperty.
#Example 1.2
$obj = New-Object PSObject
Add-Member -InputObject $obj -MemberType NoteProperty -Name customproperty -Value
""
A note about this is that you need to specify a value where as with select-object you do not.
#Example 1.3 $props = @{
Property1 =
'one'
Property2 =
'two'
Property3 =
'three'
}
$
=
psobject -Property $props
#Example 1.4 $obj = [PSCustomObject]@{
You can also use New-Module with the AsCustomObject parameter to create a custom object. The main advantage to this is the ability to type constrain a member. It is also easier to add methods (functions)
#Example 2.1 $numbers = New-Module -AsCustomObject -ScriptBlock {[
int
]$n1=$
null
Export-ModuleMember -Variable *}
$numbers.n1 = 4
$numbers.n1 =
"abc"
If you test the above code you'll see that it will throw an error when you try to assign the string abc to it. You can also use functions inside the scriptblock to act as methods for the object.
#example 2.2 $numbers = New-Module -AsCustomObject -ScriptBlock {
[
Function Sqr {
[math]::pow($n1,2)
Export-ModuleMember -Variable * -Function *}
$numbers.n1 = 5
$numbers.Sqr()
In this case forcing N1 to be a number allows you to write less validation code in the Sqr function.
In some rare cases the Add-Type cmdlet can be useful for creating a type by defining a class in C#. This provides you with a type name and access to the C# runtime. One of the main reasons to do this is to get access to the Win32_API calls.
#Example 3.1 Add-Type @"
using
System;
public
class
myClass{
Double number=0;
Double Sqr()
{
return
Math.Pow(number,2.0);
"@
$obj = New-Object myClass
$obj.number = 5
$obj.Sqr()
#example 4.1 $obj =
| select prop1, prop2
#Example 4.2 $obj | gm
TypeName: Selected.System.String
Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() prop1 NoteProperty prop1=null prop2 NoteProperty prop2=null You'll notice that the object type is a Selected.System.String.
After creating a custom object, you can update the typename of the object to reference something else. This works great when using formatting files (.ps1xml) to change how the data is displayed for an object. This works with both using New-Object and the Select-Object methods described above. The most common method of adding a typename is the Insert() method. This method allows you to insert the typename at a specified index of the collection. You can also use the Add() method, but this will add the typename at the bottom of the collection. It is important to know that this will not add anything extra to the object, so you cannot turn an object into a hashtable with its associated methods just by inserting the hashtable typename into the object. Examples:
#Example 5.1 #Create the custom object
#Update the typename to something else and place it at the first index of the collection
$obj.pstypenames.insert(0,
'Custom.ObjectExample'
)
#Look at object
$obj |
get
-member
TypeName: Custom.ObjectExample
Name MemberType Definition
---- ---------- ----------
Equals Method
bool
Equals(System.Object obj)
GetHashCode Method
GetHashCode()
GetType Method type GetType()
ToString Method
string
ToString()
prop1 NoteProperty prop1=
prop2 NoteProperty prop2=
#Example 5.2 #Create custom object
= New-Object PSObject
#Add a custom typename to the object
.pstypenames.insert(0,
'System.CustomObject.PSObject'
#Display object
$Object | Get-Member
TypeName: System.CustomObject.PSObject
are bee en zed edited Revision 20. Comment: Fixed typo.
Ed Price - MSFT edited Revision 19. Comment: Title guidelines (minor)
Richard Mueller edited Revision 18. Comment: Removed (en-US) from title, added tags
jrich edited Revision 16. Comment: crappy editor!
jrich edited Revision 14. Comment: indexed and added exmple numbers
Peter Kriegel edited Revision 12. Comment: Added Blog Links
Richard Mueller edited Revision 9. Comment: Added detail on the difference between TypeNames PSObject and Object
Patris_70 edited Revision 8. Comment: added en-US tag and title
jrich edited Revision 7. Comment: added add-type and add-module methods for creating objects
jrich edited Revision 6. Comment: Added V3 method
jrich edited Original. Comment: added link to parent document at the top
Boe Prox edited Revision 2. Comment: Updated comment about Select-Object and being able to update the typename.
Boe Prox edited Revision 3. Comment: Add content on using custom typenames
Boe Prox edited Revision 5. Comment: Corrected type name for returned Select-Object method
Good Stuff..May be a Gallary item?