Pwershell Get-Member vs -Properties * vs Overloading example: []::Method()

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Nomad Deamons Nomad Deamons 6 months, 2 weeks ago.

  • Author
    Posts
  • #39268
    Profile photo of Nomad Deamons
    Nomad Deamons
    Participant

    Pwershell Get-Member vs -Properties * vs Overloading example: []::Method()

    I am barely Beginning to try powershell and I do not understand the three concepts above.

    For example if one issues: get-aduser -filter {Name -like 'aduser'} | gm
    the results are all the members the of the object, which are very few in comparison with the results obtained from:
    get-aduser -filter {Name -like 'aduser'} -Proeprties *

    So the question would be why Get-Member cannot show all the properties of the object?

    In addition can anyone explain overloading? [system.Microsoft.whatever]::Method() vs having a $variable.Method() But I guess the second is instantiating the object.

    And when one can use the get and set methods -> example System.String UserPrincipalName {get;set;} on an object? for example when i can use the get on get-aduser to get a certain property, or this can be only used if the the object is put inside a variable? or only pipe it trough select-object?
    Thank you.

  • #39269
    Profile photo of Paul Frankovich
    Paul Frankovich
    Participant

    For the Get-ADUser part the reason Get-Member seems to show fewer attributes than adding the -Properties * is because the cmdlet does not load all the object properties by default. Adding the Properties parameter with the * is telling the cmdlet to load all the properties.

  • #39271
    Profile photo of Don Jones
    Don Jones
    Keymaster

    "So the question would be why Get-Member cannot show all the properties of the object?"

    As Paul notes, Get-ADUser doesn't load all of the properties by default. Its behavior is designed to reduce load on the domain controller. You can use -Properties to specify additional properties you want to query, or * to query all of them (for a performance hit).

    "In addition can anyone explain overloading? [system.Microsoft.whatever]::Method() vs having a $variable.Method() But I guess the second is instantiating the object."

    Neither of these is overloading. The first is calling a static method of a class. The second, presuming $variable contains an instance of the class, is calling an instance method. The second does presume that the class has been instantiated.

    "And when one can use the get and set methods -> example System.String UserPrincipalName {get;set;} on an object? for example when i can use the get on get-aduser to get a certain property, or this can be only used if the the object is put inside a variable? or only pipe it trough select-object?"

    You would generally not use the methods to manipulate the object – you would, in this example, use Set-ADUser. The Get/Set you're seeing indicates that the property is readable (get) and writable (set), but the convention in PowerShell is to use cmdlets to perform those tasks.

  • #39278
    Profile photo of Dan Potter
    Dan Potter
    Participant

    get-aduser user -Properties * |gm

    Typically when I use get-member I'm looking for methods only for which cmdlets don't already exist.

    The definitions are also included with the methods, basically instructions on how to use the method.

    ex. I manage my itunes library through powershell. Using get-member of my libraryplaylist I find the search,addfile,playfirsttrack... methods and what arguments are required. I couldn't possibly figure out what to do with just the properties output

    Search Method IITTrackCollection Search (string, ITPlaylistSearchField)

    $libraryplaylist.search($genre, $this.ITPlaylistSearchField.ITPlaylistSearchFieldgenre)

  • #39295
    Profile photo of Nomad Deamons
    Nomad Deamons
    Participant

    Thank you very much.

You must be logged in to reply to this topic.