System.Object or PSObject

Welcome Forums General PowerShell Q&A System.Object or PSObject

This topic contains 5 replies, has 3 voices, and was last updated by

 
Participant
1 year, 3 months ago.

  • Author
    Posts
  • #76118

    Participant
    Points: 0
    Rank: Member

    I have recently started using custom objects inside a few of my PowerShell scripts. Upon looking around online I noticed that there are two ways to create these objects

     $Obj1 = New-Object System.Object 
     $Obj2 = New-Object PSObject 

    They both have the functionality that I needed, such as setting custom properties for necessary information to be held.

    My question is, is there a necessary time to use one of them over the other? I found an article on Technet that seems to be heading in the right direction to giving an answer to this question, but I haven't been able to wrap my mind around it. Link is below.

    Technet Link

    Thanks!

  • #76121

    Keymaster
    Points: 12
    Team Member
    Rank: Member

    Hey there Cory,

    This might help a little more in getting your head wrapped around it.

    Custom object gotchas

    • #76123

      Participant
      Points: 0
      Rank: Member

      Thanks Will! That article definitely helped out.

  • #76124

    Keymaster
    Points: 12
    Team Member
    Rank: Member

    No problem! Also, if you're looking at creating custom objects, you might want to check out PSCustomObject.

  • #76132
    js

    Participant
    Points: 218
    Helping Hand
    Rank: Participant

    I see this shorthand a lot:

    PS C:\> [pscustomobject]@{name='joe';address='here'}
    
    name address
    ---- -------
    joe  here
    
  • #76147

    Participant
    Points: 0
    Rank: Member

    I saw the PSCustomObject once before yes. I believe something along these lines, pretty close to the example js posted.

    $obj = [PSCustomObject]@{Property1=a; Property20=b}

The topic ‘System.Object or PSObject’ is closed to new replies.