{"id":3311,"date":"2009-12-04T20:30:25","date_gmt":"2009-12-04T20:30:25","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2009\/12\/04\/new-object-psobject-property-hashtable\/"},"modified":"2019-02-18T13:06:04","modified_gmt":"2019-02-18T20:06:04","slug":"new-object-psobject-property-hashtable","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/new-object-psobject-property-hashtable\/","title":{"rendered":"New-Object PSObject &#8211;Property [HashTable]"},"content":{"rendered":"<pre class=\"PowerShellColorizedScript\">Let me start of by suggesting you take a few minutes and read Laerte Junior\u2019s excellent blog <br \/>entry <a href=\"http:\/\/www.simple-talk.com\/content\/article.aspx?article=874\">Exceptional PowerShell DBA Pt1 Orphaned Users<\/a>.  Laerte does a great job describing the <br \/>scenario, the approach he took to solving it and then he included the scripts!  Really good<br \/>stuff Laerte! <\/pre>\n<pre class=\"PowerShellColorizedScript\">When I looked at Laerte\u2019s code, I recognized one of my least favorite PowerShell V1isms:<\/pre>\n<pre class=\"PowerShellColorizedScript\">\t\t<span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Object<\/span> <span style=\"color: #8a2be2\">PSObject<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">LineNumber<\/span>       <span style=\"color: #ff4500\">$LineNumber<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">Date<\/span>             <span style=\"color: #ff4500\">$TodayDate<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">ServerName<\/span>       <span style=\"color: #ff4500\">$svr<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">DatabaseName<\/span>     <span style=\"color: #ff4500\">$Database<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">UserName<\/span>         <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">name<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">CreateDate<\/span>       <span style=\"color: #ff4500\">$CreateDate<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">DateLastModified<\/span> <span style=\"color: #ff4500\">$DateLastModified<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">AsymMetricKey<\/span>    <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AsymMetricKey<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">DefaultSchema<\/span>    <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">DefaultSchema<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">HasDBAccess<\/span>      <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">HasDBAccess<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">ID<\/span>               <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ID<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">LoginType<\/span>        <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">LoginType<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">Login<\/span>            <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span>\n               <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">|<\/span> <span style=\"color: #0000ff\">add-member<\/span> <span style=\"color: #8a2be2\">Noteproperty<\/span> <span style=\"color: #8a2be2\">Orphan<\/span>           <span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span> <span style=\"color: #a9a9a9\">-eq<\/span> <span style=\"color: #8b0000\">&quot;&quot;<\/span><span style=\"color: #000000\">)<\/span>\n                                <\/pre>\n<p>Gosh that is a horrible looking thing.&#160; This is one of the things we fixed with PowerShell V2 so you take a few minutes to\n  <br \/>learn this so you never have to do that mess again.&#160; Let\u2019s get some help:\n  <\/p>\n<p><font size=\"2\" face=\"Courier New\">PS&gt; G<strong>et-Help New-Object -Parameter Property<\/strong> <\/font><\/p>\n<p><font size=\"2\" face=\"Courier New\">-Property &lt;hashtable&gt;\n    <br \/>&#160;&#160;&#160; Sets property values and invokes methods of the new object.\n    <br \/>&#160;&#160;&#160; Enter a hash table in which the keys are the names of properties or methods and the values are property value\n    <br \/>&#160;&#160;&#160; s or method arguments. New-Object creates the object and sets each property value and invokes each method in\n    <br \/>&#160;&#160;&#160; the order that they appear in the hash table.\n    <br \/>&#160;&#160;&#160; If the new object is derived from the PSObject class, and you specify a property that does not exist on the o\n    <br \/>&#160;&#160;&#160; bject, New-Object adds the specified property to the object as a NoteProperty. If the object is not a PSObjec\n    <br \/>&#160;&#160;&#160; t, the command generates a non-terminating error.\n    <br \/>&#160;&#160;&#160; Required?&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; false\n    <br \/>&#160;&#160;&#160; Position?&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; named\n    <br \/>&#160;&#160;&#160; Default value&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br \/>&#160;&#160;&#160; Accept pipeline input?&#160;&#160;&#160;&#160;&#160;&#160; false\n    <br \/>&#160;&#160;&#160; Accept wildcard characters?&#160; false<\/font> <\/p>\n<p>&#160; <\/p>\n<p>Thats right \u2013 you can now specify a hashtable and we\u2019ll set these as NoteProperties on an object.&#160; What that means is that you can now write code like this: <\/p>\n<blockquote>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">    $Object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Object<\/span> <span style=\"color: #8a2be2\">PSObject<\/span> <span style=\"color: #000080\">-Property<\/span> <span style=\"color: #000000\">@{<\/span>\n        <span style=\"color: #000000\">LineNumber<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$LineNumber<\/span>\n        <span style=\"color: #000000\">Date<\/span>             <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$TodayDate<\/span>\n        <span style=\"color: #000000\">ServerName<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$svr<\/span>\n        <span style=\"color: #000000\">DatabaseName<\/span>     <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$Database<\/span>\n        <span style=\"color: #000000\">UserName<\/span>         <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">name<\/span>\n        <span style=\"color: #000000\">CreateDate<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$CreateDate<\/span>\n        <span style=\"color: #000000\">DateLastModified<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$DateLastModified<\/span>\n        <span style=\"color: #000000\">AsymMetricKey<\/span>    <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AsymMetricKey<\/span>\n        <span style=\"color: #000000\">DefaultSchema<\/span>    <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">DefaultSchema<\/span>\n        <span style=\"color: #000000\">HasDBAccess<\/span>      <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">HasDBAccess<\/span>\n        <span style=\"color: #000000\">ID<\/span>               <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ID<\/span>\n        <span style=\"color: #000000\">LoginType<\/span>        <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">LoginType<\/span>\n        <span style=\"color: #000000\">Login<\/span>            <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span>\n        <span style=\"color: #000000\">Orphan<\/span>           <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span> <span style=\"color: #a9a9a9\">-eq<\/span> <span style=\"color: #8b0000\">&quot;&quot;<\/span><span style=\"color: #000000\">)<\/span>\n    <span style=\"color: #000000\">}<\/span>\n                                    <\/pre>\n<\/blockquote>\n<p>or if you prefer:<\/p>\n<blockquote>\n<pre class=\"PowerShellColorizedScript\"><span style=\"color: #ff4500\">   $hash<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">@{<\/span>\n        <span style=\"color: #000000\">LineNumber<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$LineNumber<\/span>\n        <span style=\"color: #000000\">Date<\/span>             <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$TodayDate<\/span>\n        <span style=\"color: #000000\">ServerName<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$svr<\/span>\n        <span style=\"color: #000000\">DatabaseName<\/span>     <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$Database<\/span>\n        <span style=\"color: #000000\">UserName<\/span>         <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">name<\/span>\n        <span style=\"color: #000000\">CreateDate<\/span>       <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$CreateDate<\/span>\n        <span style=\"color: #000000\">DateLastModified<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$DateLastModified<\/span>\n        <span style=\"color: #000000\">AsymMetricKey<\/span>    <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">AsymMetricKey<\/span>\n        <span style=\"color: #000000\">DefaultSchema<\/span>    <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">DefaultSchema<\/span>\n        <span style=\"color: #000000\">HasDBAccess<\/span>      <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">HasDBAccess<\/span>\n        <span style=\"color: #000000\">ID<\/span>               <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">ID<\/span>\n        <span style=\"color: #000000\">LoginType<\/span>        <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">LoginType<\/span>\n        <span style=\"color: #000000\">Login<\/span>            <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span>\n        <span style=\"color: #000000\">Orphan<\/span>           <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #000000\">(<\/span><span style=\"color: #ff4500\">$user<\/span><span style=\"color: #a9a9a9\">.<\/span><span style=\"color: #000000\">Login<\/span> <span style=\"color: #a9a9a9\">-eq<\/span> <span style=\"color: #8b0000\">&quot;&quot;<\/span><span style=\"color: #000000\">)<\/span>\n    <span style=\"color: #000000\">}<\/span>\n    <span style=\"color: #ff4500\">$Object<\/span> <span style=\"color: #a9a9a9\">=<\/span> <span style=\"color: #0000ff\">New-Object<\/span> <span style=\"color: #8a2be2\">PSObject<\/span> <span style=\"color: #000080\">-Property<\/span> <span style=\"color: #ff4500\">$hash<\/span>            <\/pre>\n<\/blockquote>\n<p>&#160;<\/p>\n<p>This is SO much better than V1 \u2013 be sure to use it and to tell all your friends about it as well!&#160; <\/p>\n<p>Experiment!&#160; Enjoy!&#160; Engage! <\/p>\n<p>Jeffrey Snover [MSFT]\n  <br \/>Distinguished Engineer\n  <br \/>Visit the Windows PowerShell Team blog at:&#160;&#160;&#160; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a>\n  <br \/>Visit the Windows PowerShell ScriptCenter at:&#160; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let me start of by suggesting you take a few minutes and read Laerte Junior\u2019s excellent blog entry Exceptional PowerShell DBA Pt1 Orphaned Users. Laerte does a great job describing the scenario, the approach he took to solving it and then he included the scripts! Really goodstuff Laerte! When I looked at Laerte\u2019s code, I [&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":[],"class_list":["post-3311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Let me start of by suggesting you take a few minutes and read Laerte Junior\u2019s excellent blog entry Exceptional PowerShell DBA Pt1 Orphaned Users. Laerte does a great job describing the scenario, the approach he took to solving it and then he included the scripts! Really goodstuff Laerte! When I looked at Laerte\u2019s code, I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3311","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=3311"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/3311\/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=3311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=3311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=3311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}