PowerShell shortest syntax to create a new PSObject using Property?

2.4k views Asked by At

I use the following very often:

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:

#{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.

1

There are 1 answers

4
Cookie Monster On BEST ANSWER

PowerShell 3 and later support this syntax:

[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.