PowerTip: The Easy Way to Create a Custom PowerShell Object


Summary: Create a custom Windows PowerShell object with the PSCustomObject type accelerator.

Hey, Scripting Guy! Question What’s the easiest way to create a custom object?

Hey, Scripting Guy! Answer Charlotte Windows PowerShell User group member Brian Wilhite says: One of the easiest ways to create a custom object is to use the PSCustomObject type accelerator.  This “shortcut” will not only create the custom object, but it will arrange the key/value pairs in the order in which they were entered.

$CustomObject = [PSCustomObject]@{a=1;c=3;b=2;d=4}

$CustomObject | Get-Member

 

Comments (2)

  1. Gary says:

    PS3 only, but write brief json description and import the object.

  2. chandru says:

    another easy way
    $CustomObject = [PSCustomObject]"" | select a,b,c,d

Skip to main content