Add-Member enhancements in PowerShell 3.0

April 11, 2012

24 comments

PowerShell 3.0 offers new ways to add Note properties to objects. In PowerShell 2.0, a typical command to do that would look like:

PS> New-Object -TypeName PSObject | 
Add-Member -MemberType NoteProperty -Name One -Value 1 -PassThru

One
---
  1

 

And when adding multiple note properties:

PS> New-Object -TypeName PSObject | 
      Add-Member -MemberType NoteProperty -Name One -Value 1 -PassThru | 
      Add-Member -MemberType NoteProperty -Name Two -Value 2 -PassThru | 
      Add-Member -MemberType NoteProperty -Name Three -Value 3 -PassThru


One Two Three
--- --- -----
  1   2     3

As you can see the syntax was very long and each time you created a NoteProperty you had to specify that. In Powershell 3.0 Add-Member have to new parameters: NotePropertyName and NotePropertyValue. Now you can easily add a NoteProperty without having to specify the member type and without having to specify parameter names, we take advantage of their positional value.

PS> New-Object -TypeName PSObject | Add-Member One 1

One
---
  1

 

To add multiple NoteProperty we can now use a hashtable:

PS> New-Object -TypeName PSObject | Add-Member @{One=1; Two=2; Three=3} -PassThru

One Three Two
--- ----- ---
  1     3   2

Notice that the object we got back didn’t preserve the order of properties defined in the hashtable. Order in a dictionary is defined the moment you create it and the regular hashtable syntax @{} is an unordered dictionary. To make sure properties are listed in the order we defined them, we use an ordered hashtable.

PS> $pso = New-Object -TypeName PSObject
PS> $pso | Add-Member ([ordered]@{One=1; Two=2; Three=3}) -PassThru One Two Three --- --- ----- 1 2 3

 

Finally, there’s another interesting new parameter: TypeName. We can use it to specify a name for the resultant object type. This is very useful when you use format files to define the default display of your objects. Previously we had to add the type name to the object’s PSTypeNames list, now it is even easier.

PS> $pso = New-Object -TypeName PSObject 
PS> $pso | Add-Member ([ordered]@{One=1; Two=2; Three=3}) -TypeName MyType.MyObject 
PS> $pso | Get-Member TypeName: MyType.MyObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() One NoteProperty System.Int32 One=1 Three NoteProperty System.Int32 Three=3 Two NoteProperty System.Int32 Two=2
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

24 comments

  1. DoctorFrankoFebruary 18, 2013 ב 20:32

    [url=http://medpillstoday.com/buy-zithromax-usa.html][img]http://i.imgur.com/B0wUD.jpg[/img][/url]

    Azithromycin no prescription non generic
    order Azithromycin online
    Acheter Azithromycin en ligne
    Azithromycin non generic
    buy Azithromycin online
    Azithromycin canadian pharmacy
    kaufen Azithromycin online
    discount Azithromycin
    buy Azithromycin internet pharmacy
    buy Azithromycin
    buy Azithromycin jelly
    20 mg Azithromycin
    Azithromycin 10mg online
    discounted Azithromycin
    50 mg Azithromycin
    10 mg Azithromycin
    comprar Azithromycin online
    buy cheap Azithromycin online
    generic Azithromycin paypal
    order Azithromycin online
    Buy Azithromycin online
    online prescriptions no required Azithromycin
    Comprar Azithromycin en linea
    lowest Azithromycin price online
    Azithromycin 20mg without prescription
    Discount Azithromycin
    Azithromycin professional online
    Osta Azithromycin
    Azithromycin discounted
    order Azithromycin
    Azithromycin tadalafil
    Azithromycin discount price
    buy Azithromycin 10mg online
    cheap Azithromycin
    acheter Azithromycin en ligne

    http://kenax.net/memberlist.php?mode=viewprofile&u=45806

    Reply
  2. http://mssoftware.de/louisvuittontaschen.htmlJuly 23, 2013 ב 17:15

    Malaysia. Maldive. Mali. There is room for every sort of creature in its 25,000 acres, making it the second-largest contiguous landholding by the state. The Osmore Pond Loop is a hiking trail that encircles the shores of the pond for about 2 miles. The path is clearly blazed in blue and has a few wet areas with narrow footbridges to cross. Louis Vuitton Tasche Gunstig http://mssoftware.de/louisvuittontaschen.html Louis Vuitton Tasche Gunstig

    Reply
  3. http://scrapebox.pink2.net/July 27, 2013 ב 11:18

    worldwide hotels in one click New flight connections for Petit St Vincent

    Reply
  4. http://scrapebox.pink2.net/July 27, 2013 ב 11:23

    88 ARYAN AIR TANIE LINIE LOTNICZE

    Reply
  5. http://www.wantoakleys.comJuly 27, 2013 ב 11:33

    Careers at Corporate iRockProDomain Names, Web Hosting and SSL Certificates

    Reply
  6. http://www.goodhermesbag.info/July 27, 2013 ב 12:14

    Fashion Blogger in der ersten Reihe Fashion Lifestyle Blog

    Reply
  7. http://cfnhri.org/cheapnikefreeruns.html]August 13, 2013 ב 12:13

    Nonetheless even an unintended landlord must realize their lawful duties, irrespective of whether they will use a realtor or accept the home and property operations themselves. A budget Jordan Spiz superior includes mostly dark clair leather. The majority of us has learned Nike, from children to olders, expecially NBA admirers, the total is more deeply than other people.

    Reply
  8. lashawndabyrer928August 16, 2013 ב 17:14

    auction handful of those people obtained can be available All of us that you ought to [URL=http://www.fabbelt.com/mens-belts-gucci-mens-belts-cheap-1_7.html]Gucci Men’s Belts[/URL]
    extensive sun dallas, texas colorado front range china’s websites low cost spectacles eyewear photo frames a man economical interbank sun glasses sunglasses merchants your next sunglasses on-line the bottom at tinted glasses treatments with regards to burgandy glasses discount big apple All above told the truth. We can communicate on this theme.
    You are absolutely right. In it something is also I think, what is it excellent idea.

    when you
    . This
    [URL=http://www.fabbelt.com/mens-belts-hermes-mens-belts-cheap-1_6.html]Hermes Mens Belts outlet[/URL]
    oakley below wholesale spectacles general sun shades shop cheaper dark glasses from wholesale prices sun glasses in bulk chicago roman colorations at wholesale prices artist spectacles 12 eyeglasses eye glasses accessories

    You are absolutely right. In it something is also to me it seems it is excellent idea. I agree with you.
    [URL=http://www.fabbelt.com/women’s-belts/louis-vuitton-womens-belts/louis-vuitton-white-smooth-leather-lv-letters-buckle-belt-on-sale-1177.html]LOUIS VUITTON WHITE SMOOTH LEATHER LV LETTERS BUCKLE BELT[/URL]
    oakley below wholesale eyeglasses eye protection males low price extensive shades tiongkok window shades

    Reply