Create PowerShell Object

Hello,

Hereunder a little reminder about how to create object in all version of PowerShell :

PowerShell 1

#PowerShell v1
$Computer = "" | select Name, State, OS
$Computer.Name = "Localhost"
$Computer.State = "Unknown"
$Computer.OS = "Unknown"
$Computer

PowerShell-Object-v1

PowerShell 2

#PowerShell v2
$Computer = New-Object -TypeName PSObject -Property @{Name = "Localhost"; State = "Unknown"; OS = "Unknown"}
$Computer

PowerShell-Object-v2

PowerShell 3

#PowerShell v3
[PsCustomObject]@{Name = "Localhost"; State = "Unknown"; OS = "Unknown"}

PowerShell-Object-v3

If you look at performances :

PowerShell-Object-Performances

Please upgrade to the latest PowerShell version you can if you want to have maximum performances.

0 thoughts on “Create PowerShell Object

  1. Pingback: Add custom type to a PowerShell Custom Object - It For DummiesIt For Dummies

  2. Pingback: Add custom method to custom object - It For DummiesIt For Dummies

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.