3 ways of object creation in Powershell

1. Using Select-Object

$myobject = "" | select name, number, description
$myobject.Name = "Object1"
$myobject.Number = 100
$myobject.Description = "Using Select-Object"

ADMIN PS> $myobject | fl
name        : Object1
number      : 100
description : Using Select-Object

ADMIN PS> $myobject | gm

   TypeName: Selected.System.String

Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
description NoteProperty System.String description=Using Select-Object
name        NoteProperty System.String name=Object1
number      NoteProperty System.Int32 number=100

ADMIN PS> $myobject.gettype().fullname
System.Management.Automation.PSCustomObject

 

2.Using New-Object and Add-Member

$myobject = New-Object System.Management.Automation.PSObject |
Add-Member -MemberType NoteProperty -Name "Name" `
-Value "Object2" -PassThru |
Add-Member -MemberType NoteProperty -Name "Number" `
-Value 100 -PassThru |
Add-Member -MemberType NoteProperty -Name "Description" `
-Value "Using New-Object and Add-Member" -PassThru

 

ADMIN PS> $myobject | fl
Name        : Object2
Number      : 100
Description : Using New-Object and Add-Member

ADMIN PS> $myobject | gm
  TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Description NoteProperty System.String Description=Using New-Object and Add-Member
Name        NoteProperty System.String Name=Object2
Number      NoteProperty System.Int32 Number=100

ADMIN PS> $myobject.gettype().fullname
System.Management.Automation.PSCustomObject

3. Using New-Object and a hashtable

$myobject = New-Object System.Management.Automation.PSObject `
-Property @{
Name = "Object3"
Number= 100
Description = "Using New-Object and a hashtable"
}

ADMIN PS> $myobject | fl
Name        : Object3
Number      : 100
Description : Using New-Object and a hashtable

ADMIN PS> $myobject | gm
TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition
—-        ———-   ———-
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()
GetType     Method       type GetType()
ToString    Method       string ToString()
Description NoteProperty System.String Description=Using New-Object and a hashtable
Name        NoteProperty System.String Name=Object3
Number      NoteProperty System.Int32 Number=100

ADMIN PS> $myobject.gettype().fullname
System.Management.Automation.PSCustomObject

Advertisements

Tagged:

One thought on “3 ways of object creation in Powershell

  1. Chris 22.12.2012 at 08:10 Reply

    Outstanding, I have been working on this pscustomobject creation on v3 for the better part of a week!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?

%d bloggers like this: