Question:
I use the following very often:
1 2 |
New-Object psobject -Property @{a=1; b=2; c=3; d=4} |
I would like to make this as short as possible, perhaps even 1 character?
What’s the shortest way to create a new psobject using the properties above?
My desire is that I could do it with a syntax similar to this:
1 2 |
#{a=1; b=2; c=3; d=4} |
Note that I replaced the @ with a new ‘special’ symbol.
Note, if this is considered off-topic, I will move it.
Answer:
PowerShell 3 and later support this syntax:
1 2 |
[pscustomobject]@{a=1; b=2; c=3; d=4} |
One of the added benefits is that the properties are ordered, unlike a standard call to New-Object.
You add overhead, but presumably you could create your own type accelerator that does something similar to pscustomobject, and use a shorter name : ) Not sure what goes on under the hood of pscustomobject though.