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?
CodePudding user response:
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.