How to Create an Object in PowerShell

How to Create an Object in PowerShell

Rate This
  • Comments 14

Today someone in Xbox Live Operations (the folks that keep xBox Live alive and well) pinged me with a simple question about PowerShell with a complicated answer: “How do I create a class in PowerShell?”

There’s two scenarios I find where people want classes.  The first (and most common) is familiarity: the person wants to make a class in PowerShell because they think of problems in terms of classes.  The second is practical: the person wants to make a collection of properties and methods, because that’s what the pipeline in PowerShell plays nicely with (for instance, you can Group, Sort, or Select easily on properties on an object, but not as easily on a hashtable).

There are three ways you can do this with PowerShell V2 that I know of.  You can using Add-Type to compile C# or other .NET languages directly, and this is the only way you can make a real class definition in PowerShell V2.  You can also use New-Module with -asCustomObject to export the functions and variables within a dynamic module as a custom object, but you should be aware that complicated parameter sets do not work well as script methods.  Finally, you can do things the old V1 way, which is by tacking properties and methods onto an object with the Add-Member cmdlet.  Here's the same object, built in 3 different ways:

# You can compile a class with C# or other .NET languages in PowerShell v2            
Add-Type @'
public class MyObject
    public int MyField = 5;    
    public int xTimesMyField(int x) {
        return x * MyField;
$object = New-Object MyObject            
# You can also use -asCustomObject with the New-Module cmdlet to export a module as a class            
$object = New-Module {            
    [int]$myField = 5            
    function XTimesMyField($x) {            
        $x * $myField            
    Export-ModuleMember -Variable * -Function *                
} -asCustomObject            
# You can also simply declare an object and start tacking on properties and methods with the            
# Add-Member cmdlet.  If you use -passThru you can make one giant pipeline that adds all of the            
# members and assign it to a variable            
$object = New-Object Object |            
    Add-Member NoteProperty MyField 5 -PassThru |             
    Add-Member ScriptMethod xTimesMyField {            
        $x * $this.MyField            
        } -PassThru            

Hope this Helps

James Brundage [MSFT]

Leave a Comment
  • Please add 8 and 4 and type the answer here:
  • Post
  • Creates a PowerShell object with an empty command pipeline. You must call this method to create the PowerShell object. This method is introduced in Windows PowerShell 2.0.

  • Wow, this is really interesting. I may talk about it in my blog in the future. I thought the only way to create a class is through custom Cmdlet, but I guess I was wrong about it.

  • Jeffrey shows how to create custom objects -

  • Jeffrey shows how to create custom objects -

  • Thank you for submitting this cool story - Trackback from DotNetShoutout

  • Do you support extension methods in this way?  Like can I create an object using way #1 above that adds methods to another object already created?

  • Hi James

    This is all well and fine, but the objects are different.

    The first is of type: MyObject System.Object

    The second is of type: System.Management.Automation.PSCustomObject System.Object

    And the third is of type: System.Object

    So only the first get a 'real' name that can be used for testing and output formatting.

    In this respect, I have a question: I've seen there two methods to change the object type. Are they supported or can you recommend other methods:



    Best regards

  • God bless you! That last section about creating a new object and populating its members and properties is what I've been seeking for weeks!!

  • Hi Folks

    Thx for the snippets. Very useful, that stuff you show.

    A side note on Add-Type:

    If you dont want an error from calling Add-Type multiple times with the same class:

    $classDef = @' ... '@

    trap [Exception]


       Add-Type $classDef


  • Ups, Sorry Admin ^^''

    Thanks for sharing your comment! If your comment doesn't appear right away, please be patient as it may take a few minutes to publish or may require moderation.

  • This has been made much nicer in Powershell 2.


  • I have changed from method 3 to method 2. I love it! It is so much cleaner and more compact. Other admins can read it much easier and understand what is going on. Thanks!

  • Update to PS 3 or delete this post, if you heve no historical Mission!

  • :)

Page 1 of 1 (14 items)