Skip to main content

What is the difference between –Property and –ExpandProperty in Select-Object


This is often a source of confusion when someone is new to PowerShell.  Since PowerShell is used mostly be non-programers, they often do not understand what an object is.  You need to have a basic understanding of objects to know how these two difference parameters work.  Let me give you an example.
PS C:\> Get-ADUser -Identity AdminUser
 
 
DistinguishedName : CN=AdminUser,CN=Users,DC=Adatum,DC=com
Enabled           : True
GivenName         :
Name              : AdminUser
ObjectClass       : user
ObjectGUID        : 696591fc-6697-4d93-b624-3ef7de206ee9
SamAccountName    : AdminUser
SID               : S-1-5-21-817349643-1871075972-606077769-500
Surname           :
UserPrincipalName :
 
 
Here you can see a subset of a user object.  This object is displaying 10 properties in the left hand column with their associated values on the right.  Let’s call the Get-Type() method to see the object’s typename.
 
PS C:\> (Get-ADUser -Identity AdminUser).getType()
 
IsPublic IsSerial Name    BaseType                                                                       
-------- -------- ----    --------                                                                      
True     False    ADUser  Microsoft.ActiveDirectory.Management.ADAccount 
 
We can see that this is an ADUser object.  Pipe it to Get-Member
    TypeName: Microsoft.ActiveDirectory.Management.ADUser
 
Name              MemberType            Definition                                                                                       
----              ----------            ----------                                                                                       
Contains          Method                bool Contains
Equals            Method                bool Equals(S
GetEnumerator     Method                System.Collec
GetHashCode       Method                int GetHashCo
GetType           Method                type GetType(
ToString          Method                string ToStri
Item              ParameterizedProperty Microsoft.Act
DistinguishedName Property              System.String
Enabled           Property              System.Boolea
GivenName         Property              System.String
Name              Property              System.String
ObjectClass       Property              System.String
ObjectGUID        Property              System.Nullab
SamAccountName    Property              System.String
SID               Property              System.Securi
Surname           Property              System.String
UserPrincipalName Property              System.String
 
Notice again that the entire object is a Microsoft.ActiveDirectory.Management.ADUser object and the property called Name is a System.String object.  Let’s explore the –Property parameter first.
PS C:\> Get-ADUser -Identity AdminUser | Select-Object -Property Name
 
Name    
----    
AdminUser
 
Now let’s pass this object to Get-Member.
PS C:\> Get-ADUser -Identity AdminUser | Select-Object -Property Name | Get-Member
 
 
   TypeName: Selected.Microsoft.ActiveDirectory.Management.ADUser
 
Name        MemberType   Definition                   
----        ----------   ----------                   
Equals      Method       bool Equals(System.Object obj)
GetHashCode Method       int GetHashCode()            
GetType     Method       type GetType()               
ToString    Method       string ToString()            
Name        NoteProperty string Name=AdminUser     
 
Notice the TypeName.  It is still a Microsoft.ActiveDirectory.Management.ADUser object, but it is prefixed with Selected.  It is only containing selected information from the original object.  You still see a property called Name with a typename of System.String.  Now let’s look at –ExpandProperty.
PS C:\> Get-ADUser -Identity AdminUser | Select-Object -ExpandProperty Name
AdminUser
 
You see the user name, but you do not see a column header.  Piping the object to Get-Member reveals the reason why:
PS C:\> Get-ADUser -Identity AdminUser | Select-Object -ExpandProperty Name | Get-Member
 
 
   TypeName: System.String
 
Name             MemberType            Definition                                                                                         
----             ----------            ----------                                                                                        
Clone            Method                System.Object
CompareTo        Method                int CompareTo(
Contains         Method                bool Contains(
CopyTo           Method                void CopyTo(in
EndsWith         Method                bool EndsWith(
Equals           Method                bool Equals(Sy
GetEnumerator    Method                System.CharEnu
GetHashCode      Method                int GetHashCod
GetType          Method                type GetType()
GetTypeCode      Method                System.TypeCod
IndexOf          Method                int IndexOf(ch
IndexOfAny       Method                int IndexOfAny
Insert           Method                string Insert(
IsNormalized     Method                bool IsNormali
LastIndexOf      Method                int LastIndexO
LastIndexOfAny   Method                int LastIndexO
Normalize        Method                string Normali
PadLeft          Method                string PadLeft
PadRight         Method                string PadRigh
Remove           Method                string Remove(
Replace          Method                string Replace
Split            Method                string[] Split
StartsWith       Method                bool StartsWit
Substring        Method                string Substri
ToBoolean        Method                bool IConverti
ToByte           Method                byte IConverti
ToChar           Method                char IConverti
ToCharArray      Method                char[] ToCharA
ToDateTime       Method                datetime IConv
ToDecimal        Method                decimal IConve
ToDouble         Method                double IConver
ToInt16          Method                int16 IConvert
ToInt32          Method                int IConvertib
ToInt64          Method                long IConverti
ToLower          Method                string ToLower
ToLowerInvariant Method                string ToLower
ToSByte          Method                sbyte IConvert
ToSingle         Method                float IConvert
ToString         Method                string ToStrin
ToType           Method                System.Object
ToUInt16         Method                uint16 IConver
ToUInt32         Method                uint32 IConver
ToUInt64         Method                uint64 IConver
ToUpper          Method                string ToUpper
ToUpperInvariant Method                string ToUpper
Trim             Method                string Trim(Pa
TrimEnd          Method                string TrimEnd
TrimStart        Method                string TrimSta
Chars            ParameterizedProperty char Chars(int
Length           Property              int Length {gePS C:\> Get-ADUser -Identity
 
We are no longer working with the original Microsoft.ActiveDirectory.Management.ADUser object.  We are working with System.String. –ExpandProperty pulled the value of the Name property out of the original object and discarded the original object.  It them placed the value of the Name property in the PowerShell pipeline.  This was a System.String object.
 
In summary, -Property removed members from an object that you do not want to use and can accept multiple values.  –ExpandProperty removes the original object, but allows the value of the single specified property to continue.
 
 
 
 
 
 
 

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.