Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell Custom Object property order issue

When creating a custom object in powershell my properties are not ordering in the same precedence I type them in (top to bottom -> left to right).

I was told to use [ordered] on my table declaration as done below;

$AllMailData += New-Object PSObject -Property [ordered]@{
            'Unique ID' = $sharedmail.PrimarySmtpAddress
            'Display Name' = $sharedmail.DisplayName
        }

However this gives me a syntax error. Can anyone suggest where I have gone wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You will get rid of the error when you enclose everything you’re passing to -Property in parenthesis ().

Also shorter way of writing this would be:

$AllMailData += [PSCustomObject]@{
            'Unique ID' = $sharedmail.PrimarySmtpAddress
            'Display Name' = $sharedmail.DisplayName
        }

[PSCustomObject] guarantees preservation of property order, where [PSObject] does not. But it seems that using ordered hashtable in your example accomplishes that, too.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading