blog: More details on Select-Object
I often to get asked what I mean by "Select-Object changes the object type." I believe this one of those things that is easier to illustrate than just explain.
First let me show the object type Change
First you will notice you have 21 items returned from Get-Member (I only included Properties for simplicity, but methods work the same way.)
Now look at what you lose using this method.
PS> ($dir = Get-item C:\temp).Gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DirectoryInfo System.IO.FileSystemInfo PS> ($dir = Get-item C:\temp | select-Object Fullname,Name,Parent).Gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False PSCustomObject System.Object Even if you specifiy PSBASE you have new object type. PS> ($dir = Get-item C:\temp).psbase.Gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True DirectoryInfo System.IO.FileSystemInfo PS> ($dir = Get-item C:\temp | select-Object Fullname,Name,Parent).psbase.Gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False PSCustomObject System.Object
First you will notice you have 21 items returned from Get-Member (I only included Properties for simplicity, but methods work the same way.)
As you can see... Effectively they are all gone on the new object. When I first experienced this.. I thought maybe the methods were just hidden... but try to access them
PS> get-item C:\temp | Get-Member -MemberType Properties | ft Name Name ---- PSChildName PSDrive PSIsContainer PSParentPath PSPath PSProvider Attributes CreationTime CreationTimeUtc Exists Extension FullName LastAccessTime LastAccessTimeUtc LastWriteTime LastWriteTimeUtc Name Parent Root Mode ReparsePoint ## After piping to Select-Object you now only have 3 PS> Get-Item C:\temp | Select-Object Name,Fullname,Parent | Get-Member -MemberType Properties | ft name Name ---- FullName Name Parent ## Now.. methods before "Conversion" PS> Get-item C:\temp | Get-Member -MemberType Methods | ft name Name ---- Create CreateObjRef CreateSubdirectory Delete Equals GetAccessControl GetDirectories GetFiles GetFileSystemInfos GetHashCode GetLifetimeService GetObjectData GetType get_Attributes get_CreationTime get_CreationTimeUtc get_Exists get_Extension get_FullName get_LastAccessTime get_LastAccessTimeUtc get_LastWriteTime get_LastWriteTimeUtc get_Name get_Parent get_Root InitializeLifetimeService MoveTo Refresh SetAccessControl set_Attributes set_CreationTime set_CreationTimeUtc set_LastAccessTime set_LastAccessTimeUtc set_LastWriteTime set_LastWriteTimeUtc ToString ## After PS> Get-item C:\temp | select-object FullName,Name,Parent | Get-Member -MemberType Methods | ft name Name ---- Equals GetHashCode GetType ToString
I want to be clear... This is not a bug. Its not even a bad idea. I find it useful, but I think it confuses a lot of people. Like most people I didn't read the help before I used it or I would have seen this excerpt from get-help Select-Object:
PS> $dir = Get-Item C:\temp PS> $dir.GetDirectories() Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 5/22/2007 1:58 PM Console d---- 5/10/2007 4:36 PM test32 PS> $dir = Get-Item C:\temp | Select-Object FullName,Name,Parent PS> $dir.GetDirectories() Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'GetDirectories'. At line:1 char:20
If you use Select-Object to select specified properties, it copies the values of those properties from the input objects and creates new objects that have the specified properties and copied values.So, the moral of the story is don't be shocked when your object changes after using Select-Object. Embrace the change :)
tshell :: Jun.10.2009 :: Active Directory, All :: No Comments »
Leave a Reply
You must be logged in to post a comment.

