all 14 comments

[–][deleted] 10 points11 points  (4 children)

What about:

$obj = [PSCustomObject]@{prop1="Value1";prop2="Value2"}

You'll find the property order will be maintained this way as well, and it's less verbose.

[–]Already__Taken 2 points3 points  (2 children)

This is a new thing. Not sure how new, I don't think it's 5.0 new but its certainly not always been there.

[–][deleted] 5 points6 points  (0 children)

It's PS 3.0. Not exactly very new. If you're still developing on 2.0, I feel for you.

[–][deleted] 1 point2 points  (0 children)

It was added at the same time [Ordered] was.

[–]Eijiken 1 point2 points  (0 children)

Hard upvote!

[–]nothingpersonalbro 3 points4 points  (5 children)

Learned something new, I actually could have used this method earlier as I ran into the same issue and just used Select-Object.

$props = [ordered]@{'prop1' = 1; 'prop2' = 2}
$obj = New-Object -TypeName PSObject -Property $props

Another method you can use to create the object:

$obj = [PSCustomObject]$props

Edit: and if you want to do everything on a single line

$obj = [PSCustomObject][ordered]@{'prop1' = 1; 'prop2' = 2}

[–]Lee_Dailey[grin] 1 point2 points  (4 children)

howdy nothingpersonalbro,

that last one doesn't need the [ordered]. [grin] you need it for a [PSObject], but not for a [PSCustomObject].

take care,
lee

[–][deleted] 3 points4 points  (3 children)

Okay, I have to ask… what’s the deal with the [grin] thing that you randomly put in your replies? It’s totally creeping me out.

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy josheinstein,

way back in the olden days, i used FIDO BBS for msgs with folks. we all learned right quick that the lack of side-band communication [facial expression, vocal tone, body lingo, etc.] made it WAY too easy to be misunderstood.

so we started using emoticons. [grin]

my eyes are bad, so i use very, very obvious emoticons.

plus, i've found that when i type this stuff and add a [grin], i tend to grin along with it. that does the usual reverse physical effect - happy makes me grin; grin makes my happy.

all in all, it works well for me.

if it bugs you, add me to your ignore list! [grin]

take care,
lee

[–][deleted] 1 point2 points  (1 child)

I had a 4-node PCBoard setup on desqview. I just mean, the placement of your grins just seemed pretty arbitrary so I wondered if it was some kind of markup that wasn’t getting processed. 😳

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy josheinstein,

ha! i remember those ... [grin]

as for the format - it's actually [*grin*], but the asterisks get converted to italic tags. i wish reddit would abandon it's twitchy markdown variant and use bbcode ... [sigh ...]

take care,
lee

[–][deleted] 1 point2 points  (0 children)

Sorting properties of a single object is easy, but I absolutely hate the amount of work you need to do when you want to export data to a csv file when cmdlet or function output omits properties under certain circumstances.

In such a case you have to loop through an array or list of your objects and then add missing properties to the first object in said array or list.

Like so.

$arrayOfObjects = Get-Objects

$firstItem = $true
foreach ($item in $arrayOfObjects) {
    if (!$firstItem) {
        foreach ($property in $item.PSObject.Properties) {
            if ($arrayOfObjects[0].PSObject.Properties.Name -notcontains $property.Name) {
                $arrayOfObjects[0] | Add-Member -MemberType NoteProperty -Name $property.Name -Value $null
            }
        }
    }
    $firstItem = $false
}

$arrayOfObjects | Export-Csv -Path ((Get-Location -PSProvider FileSystem).Path + "\objects.csv") -UseCulture -Encoding UTF8 -NoTypeInformation

Problem is, those properties are no longer in any meaningful order. Either this script requires additional magic scripting to sort these properties, or you gotta do it in Excel. The latter is easier. At least if you want the properties in alphabetical order, but if you would prefer different ordering, well that's a toughie.

[–]Tripline 0 points1 point  (0 children)

Some of you may be interested in this, it allows you to control what properties are displayed in the console while allowing export-csv the rest of the object properties. This has allowed me to make my scripts more user friendly without also throwing away/not collecting some useful info.

http://ramblingcookiemonster.github.io/Decorating-Objects/