Posts RSS Comments RSS 127 Posts and 199 Comments till now

Creating Custom Objects

halr9000 Asked “can you explain this construct for me?”

[code]$myobj = “”| select-Object Server,Result[/code]

IMO this is the simplest way to create custom objects.

Effectively what happens is you set your $myobj to a empty string and pipe to select-object.
While this may seem odd, its actually quite useful because what select-object returns is
a PSCustomObject with the properties that you specify. This allows you to create you object
with defined properties that you can fill out later in one line.

I guess the “proper” way to create a custom object is to do this.

[code]
$myobj = new-object System.Object
$myobj | add-member -membertype noteproperty -name Server -value $sname
$myobj | add-member -membertype noteproperty -name Result -value $sResult
[/code]

While this isnt that much more work… I think its harder to read so I go with my shortcut like

[code]
$myobj = “” | select-Object Server,Result
$myobj.Server = $sname
$myobj.Result = $sResult
[/code]

I will give you one warning about select-object (not related, but while I’m on this subject.) select-object does Change the object type and therefore you lose methods and properties you may expect to be there. I have seen numerous post on the news groups that have this exact problem.

If you pipe Get-ChildItem to Get-member you get tons of methods and
properties for both System.IO.FileInfo and System.IO.DirectoryInfo
[code]4# get-childitem | gm[/code]

Now pipe it to select-object and do a Get-Member
[code]
5# (get-childitem | select-object Fullname,length) | gm

TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition
—- ———- ———-
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
FullName NoteProperty System.String FullName=C:\Windows\System32\0409
length NoteProperty length=null
[/code]

Just be careful to remember this. I actually avoid using select-object to filter objects simply because of this. Instead… I just use the properties.

No Responses to “Creating Custom Objects”

  1. on 15 Jun 2007 at 11:58 amjvierra

    Actually - if you look carefully - teh object is preserved but the NoteProperty is the wrapper type that is displayed. Use $myobj.length|gm and you will see that it is still an Int32. This holds for most objects as well. Can’t say what it does to objects that are not wrpped by PS.

    Great info. So obvious that no once else has seen this. Great time saver for gatherig cusom objects to output ot Export-* CmdLets or for building custom reports.

  2. [...] Re: Need help with Array and Export-CSV Maybe a better way to to do it is to create a custom object [link] Everything I added has ############ around it $comp = get-qadcomputer | sort-object ################## $myCollection = @() ################## $count = ($comp | measure-object).count $table = new-object "string[,]" $count,3 Published Thursday, August 30, 2007 1:01 PM by microsoft.public.windows.powershell Google Group [...]

  3. [...] select-object - BSonPOSH’s article on creating custom objects [...]

  4. on 01 Oct 2007 at 2:40 amOmid

    I like it more like this:
    $myobj = [object] | select-Object Server, Result

  5. [...] Anyway, on to line 2.  Here I use the Select-Object shortcut (thanks as always to BSonPosh for it) to create a new custom object with three properties.  Then I start to assign stuff to those properties. [...]

Trackback this post | Feed on Comments to this post

Leave a Reply

CAPTCHA image