The shortest PowerShell syntax to create a new PSObject using a property?

I use the following very often:

New-Object psobject -Property @{a=1; b=2; c=3; d=4}

      

I would like to make it as short as possible, maybe 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 syntax like this:

#{a=1; b=2; c=3; d=4}

      

Note that I have replaced @ with a new special character.

Note that if this counts as off-topic I will outweigh it.

+3


source to share


1 answer


PowerShell 3 and later supports this syntax:

[pscustomobject]@{a=1; b=2; c=3; d=4}

      



One additional benefit is that the properties are ordered, as opposed to the standard New-Object call.

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's going on under the hood of pscustomobject though.

+7


source







All Articles