{"id":4381,"date":"2009-03-10T22:43:01","date_gmt":"2009-03-10T22:43:01","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/03\/10\/how-to-create-an-object-in-powershell\/"},"modified":"2019-02-18T13:12:47","modified_gmt":"2019-02-18T20:12:47","slug":"how-to-create-an-object-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/how-to-create-an-object-in-powershell\/","title":{"rendered":"How to Create an Object in PowerShell"},"content":{"rendered":"<p>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: &#8220;How do I create a class in PowerShell?&#8221;<\/p>\n<p>There&#8217;s two scenarios I find where people want classes.&#160; 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.&#160; The second is practical: the person wants to make a collection of properties and methods, because that&#8217;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).<\/p>\n<p>There are three ways you can do this with PowerShell V2 that I know of.&#160; 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.&#160; 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.&#160; Finally, you can do things the old V1 way, which is by tacking properties and methods onto an object with the Add-Member cmdlet.&#160; Here&#8217;s the same object, built in 3 different ways:<\/p>\n<\/p>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #006400\"># You can compile a class with C# or other .NET languages in PowerShell v2<\/span>\n<span style=\"color: #0000ff\">Add-Type<\/span> <span style=\"color: #8b0000\">@'\npublic class MyObject\n{\n    public int MyField = 5;\n    public int xTimesMyField(int x) {\n        return x * MyField;\n    }\n}\n'@<\/span>\n<span style=\"color: #ff4500\">$object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Object<\/span> <span style=\"color: #8a2be2\">MyObject<\/span>\n<span style=\"color: #ff4500\">$object<\/span>\n<span style=\"color: #ff4500\">$object<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">XTimesMyField<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #006400\"># You can also use -asCustomObject with the New-Module cmdlet to export a module as a class<\/span>\n<span style=\"color: #ff4500\">$object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Module<\/span> <span style=\"color: #000000\">{<\/span>\n    <span style=\"color: #008080\">[int]<\/span><span style=\"color: #ff4500\">$myField<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #800080\">5<\/span>\n    <span style=\"color: #00008b\">function<\/span> <span style=\"color: #8a2be2\">XTimesMyField<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$x<\/span><span style=\"color: #000000\">)<\/span> <span style=\"color: #000000\">{<\/span>\n        <span style=\"color: #ff4500\">$x<\/span> <span style=\"color: #a9a9a9\">*<\/span> <span style=\"color: #ff4500\">$myField<\/span>\n    <span style=\"color: #000000\">}<\/span>\n    <span style=\"color: #0000ff\">Export-ModuleMember<\/span> <span style=\"color: #000080\">-Variable<\/span> <span style=\"color: #8a2be2\">*<\/span> <span style=\"color: #000080\">-Function<\/span> <span style=\"color: #8a2be2\">*<\/span>\n<span style=\"color: #000000\">}<\/span> <span style=\"color: #000080\">-asCustomObject<\/span>\n<span style=\"color: #ff4500\">$object<\/span>\n<span style=\"color: #ff4500\">$object<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">xTimesMyField<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">)<\/span>\n<span style=\"color: #006400\"># You can also simply declare an object and start tacking on properties and methods with the<\/span>\n<span style=\"color: #006400\"># Add-Member cmdlet.  If you use -passThru you can make one giant pipeline that adds all of the<\/span>\n<span style=\"color: #006400\"># members and assign it to a variable<\/span>\n<span style=\"color: #ff4500\">$object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Object<\/span> <span style=\"color: #8a2be2\">Object<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-Member<\/span> <span style=\"color: #8a2be2\">NoteProperty<\/span> <span style=\"color: #8a2be2\">MyField<\/span> <span style=\"color: #800080\">5<\/span> <span style=\"color: #000080\">-PassThru<\/span> <span style=\"color: #a9a9a9\">|<\/span>\n    <span style=\"color: #0000ff\">Add-Member<\/span> <span style=\"color: #8a2be2\">ScriptMethod<\/span> <span style=\"color: #8a2be2\">xTimesMyField<\/span> <span style=\"color: #000000\">{<\/span>\n        <span style=\"color: #00008b\">param<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$x<\/span><span style=\"color: #000000\">)<\/span>\n        <span style=\"color: #ff4500\">$x<\/span> <span style=\"color: #a9a9a9\">*<\/span> <span style=\"color: #ff4500\">$this<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">MyField<\/span>\n        <span style=\"color: #000000\">}<\/span> <span style=\"color: #000080\">-PassThru<\/span>\n<span style=\"color: #ff4500\">$object<\/span>\n<span style=\"color: #ff4500\">$object<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">xTimesMyField<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #800080\">10<\/span><span style=\"color: #000000\">)<\/span>            <\/pre>\n<p>Hope this Helps <\/p>\n<p>James Brundage [MSFT] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: &#8220;How do I create a class in PowerShell?&#8221; There&#8217;s two scenarios I find where people want classes.&#160; The first (and most common) is familiarity: the person wants to [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[137,4],"class_list":["post-4381","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-ctp3","tag-howto"],"acf":[],"blog_post_summary":"<p>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: &#8220;How do I create a class in PowerShell?&#8221; There&#8217;s two scenarios I find where people want classes.&#160; The first (and most common) is familiarity: the person wants to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=4381"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/4381\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=4381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=4381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=4381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}