Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: System.Object or PSObject?

Tags:

powershell

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.

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/dcfa43ba-812f-4274-b5b2-caa0e2d56988/psobject-vs-systemobject?forum=ITCG

Thanks!

like image 576
cet51 Avatar asked Oct 30 '25 04:10

cet51


1 Answers

I was able to find my answers in the article linked below.

Custom-Object-Gotchas

As a side note, the answer I was looking for was that it seems PSObject is not limited as System.Object is in terms of using them with .NET functions. A System.Object will not pass custom properties to .NET functions but a PSObject will

like image 136
cet51 Avatar answered Nov 01 '25 12:11

cet51