Stupid noob question - Why does this "byvalue" work?

Welcome Forums General PowerShell Q&A Stupid noob question - Why does this "byvalue" work?

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
11 months, 3 weeks ago.

  • Author
    Posts
  • #90113

    Participant
    Points: 22
    Rank: Member

    I hate to ask a "PowerShell 101" question but I'm having a little trouble understanding how this works...

    So I create a text file "C:\names.txt" containing samaccountnames.
    Then run the following snippet:

    $names = get-content c:\names.txt
    foreach ($name in $names)
    {
    get-aduser -identity $name -property name,enabled,accountexpirationdate
    }
     

    ——————————-

    Running "help get-ADUser -full" indicates the parameter "-Identity" is the only parameter that accepts pipeline input, and it expects pipeline input 'ByValue' for the value .

    Since "get-content c:\names.txt | gm" indicates the object type is "string", how is this value being accepted by "get-ADUser" which only accepts pipeline input 'ByValue' for value ?

  • #90116

    Participant
    Points: 22
    Rank: Member

    Sorry – the type "ADUser" was stripped out of my post. Gues arrow brackets are a no-no.
    ——————–
    Running "help get-ADUser -full" indicates the parameter "-Identity" is the only parameter that accepts pipeline input, and it expects pipeline input 'ByValue' for value 'ADUser'

    Since "get-content c:\names.txt | gm" indicates the object type is "string", how is this value being accepted by "get-ADUser" which only accepts pipeline input 'ByValue' for value 'ADUser'?

  • #90119

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Not sure how you learn if you don't ask, so don't apologize ;).

    ByValue means "of the same data type." So, normally, if you were piping in a System.String, the parameter would need to be rigged to accept System.String – that match in datatype is how it makes the connection.

    Get-ADUser is a little weird. The help says the parameter accepts objects of type ADUser, but that's not strictly correct. I'm fairly certain it's rigged to accept System.Object, which means "anything," and then it figures out what you've passed it internally. That's because it accepts a really wide variety of input on that parameter, as described in the help.

    So your statement "pipeline input 'ByValue' for the value" isn't technically correct. It doesn't accept "ByValue for the value;" it just accepts "By Value." Meaning, "by data type." Once you know a parameter accepts ByValue, you have to look and see _what kind of value_ it accepts, to know what you can pipe to it.

    "Learn Windows PowerShell in a Month of Lunches" has a lovely chapter on this 😉 as well as ByPropertyName, and how they both work.

  • #90169

    Participant
    Points: 22
    Rank: Member

    Don – Thanks for the info. I guess this is a Powershell easter egg for the Get-ADUser cmdlet.

    Powershell is a bunch of fun. Every day I either read something from "Learn Windows PowerShell in a Month of Lunches (2nd Ed)", watch a Powershell jump start or one of the CBTNuggets Powershell videos.

The topic ‘Stupid noob question - Why does this "byvalue" work?’ is closed to new replies.