locked
Need an explanation of why this works, please. RRS feed

  • Question

  • So I was trying to output some data to a csv file within Powershell using the Export-CSV command.  It worked, but one of the properties (the value propertyI was exporting has arrays of values.  Because of this, the data in the CSV file would simply show up as "System.String[]"

    I know that this is because the property that it was trying to output was an array of values and could not save it that way, but I found someone that had a solution that works.  I just don't understand what some of it is doing.

    The code I was using:

    select OptionID, Name, Value

    The code he used was this:

    select OptionID, Name, @{l="Value";e={$_.Value -join " "}}

    The section after the NAME property is what is like magic to me.  Instead of using the "Value" property, they used @{l="Value";e={$_.Value -join " "}}

    The part I don't understand is how Powershell knows what l= and e= are.  Is it something that is built into properties or into Powershell itself?

    I know that the join is joinig all of the array values into a string and the segments on both sides of the ; look like its' a replace, but I can't for the life of me find anything on what l= and e= represent.  Can you do that with any property on any object?

    Wednesday, February 12, 2014 11:31 PM

Answers

All replies

  • Hi,

    Calculated properties are great:

    http://technet.microsoft.com/en-us/library/ff730948.aspx

    EDIT: One extra note, since this doesn't appear in the article I linked to - the l in your example (really is label , but can be shortened to l) is the same as Name in the article. I generally use @{n='';e={}} myself, simply because I think n is more descriptive than l.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    • Edited by Mike Laughlin Wednesday, February 12, 2014 11:48 PM
    • Proposed as answer by Jason Warren Thursday, February 13, 2014 12:41 AM
    • Marked as answer by Bartek Bielawski Sunday, February 16, 2014 5:18 PM
    Wednesday, February 12, 2014 11:43 PM
  • So I am guessing that "l" is short for "label" and "e" is short for "expression".  Is that right?

    In any case, this is one of the best articles I have seen and I'm not sure how I have lived without knowing about this so far.  Thanks for the link to the article, Mike.

    Wednesday, February 12, 2014 11:57 PM
  • So I am guessing that "l" is short for "label" and "e" is short for "expression".  Is that right?

    Yep, that's right.

    In any case, this is one of the best articles I have seen and I'm not sure how I have lived without knowing about this so far. Thanks for the link to the article, Mike.

    You're very welcome, that's probably one of the best articles I came across when I was first getting comfortable with PowerShell. Take a look through all of the articles, there's some great information there.


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Thursday, February 13, 2014 12:10 AM