none
How to pipe results from one command to another command for additional info RRS feed

  • Question

  • I want to list the users in an AD group by their first and last name and can do it in two steps per user, but is there a way to combine the two commands?  So I run this to get the usernames -

    PS D:\tools> get-adgroupmember "2ndFloorUsers" | select name | sort name

    name
    ----
    smithj
    lees
    kirbyj

    then have to go this for each member -

    PS D:\tools> get-aduser kirbyj | select surname, givenname

    surname                                               givenname
    -------                                                  ---------
    Kirby                                                    Jack

    so I would want to pipe the results of the first to the second command so it will list them by real names as opposed to the usernames.  Thx!

    Thursday, October 16, 2014 4:54 PM

All replies

  • If you have version 4 or higher, you could use the -Pipeline variable parameter. Assuming that isn't the case and that by Name you are really talking about SamAccountName which is the username, you could do the following.

    Get-ADGroupMember "2ndFloorUsers" |
    Select SamAccountName |
    Sort SamAccountName |
    ForEach-Object {Get-Aduser $_.SamAccountName |
    Select GivenName, SurName, {Name="SamAccountName";Expression={$_ | Select -ExpandProperty SamAccountName}}
    }

    Really, you may be better of breaking it up a bit.



    • Edited by Mark Ince Thursday, October 16, 2014 6:39 PM Change code to only use Get-Aduser once
    Thursday, October 16, 2014 6:34 PM
  • Thx.  That seems to work nicely.  But I actually am running Version 4.0.  How would you use the -Pipeline variable parameter.  I'm not familiar with it.

    Thursday, October 16, 2014 8:46 PM
  • With pipelinevariable, you could do something like

    Get-ADGroupMember "2ndFloorUsers" -PipelineVariable groupinfo |
    Select SamAccountName |
    Sort SamAccountName |
    Get-AdUser -Properties GivenName, SurName -PipelineVariable userinfo |
    Select-Object -Property @{label='UserName';Expression={$groupinfo.SamAccountName}}, @{label='GivenName';Expression={$userinfo.GivenName}},@{label='SurName';Expression={$userinfo.SurName}}


    I can't test the above as I don't have version 4.

    Thursday, October 16, 2014 10:16 PM
  • I tried it, but it errored out with multiple messages like below -

    Get-AdUser : The input object cannot be bound to any parameters for the command either because the command does not take

    pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

    At line:4 char:1

    + Get-AdUser -Properties GivenName, SurName -PipelineVariable userinfo |

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (@{SamAccountName=WSJM2}:PSObject) [Get-ADUser], ParameterBindingException

        + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADUser

    Friday, October 17, 2014 4:40 PM