finding members of a group and printing out name and email

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Aapeli Hietikko Aapeli Hietikko 1 month, 4 weeks ago.

  • Author
    Posts
  • #62103
    Profile photo of brian catlin
    brian catlin
    Participant

    Hi,

    I needed a power shell script that would pull all the members out of a group and pipe them to a CSV. I could get it to work if I used FT (format command to the screen but I remember we cannot pipe formatted stuff to csv)

    I figured out how to do this but it was kind of corny, and I don't think it was done properly. I don't really study powershell anymore as the last year or so has been spent getting my CCNA and now working on my CCNP. I love Power Shell and use to study it but I never got to use much of it at work which is why I pushed hard in the network direction.

    Anyway my code below worked but but put a bunch of crud into the csv and I cheated by then saving it as a Excel workbook and just deleting out the columns I did not want.

    Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv

    What would be a much cleaner and better way to do this where my outputted information was only the properties that I wished instead of all the generic properties?

  • #62104
    Profile photo of Gary
    Gary
    Participant

    If I understand correctly, you just want the Name,Email in the CSV?
    You can pipe to select.

    Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | Select name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv

    I personally don't like piping everything in a script. I would write it like this, but i'm sure someone else could probably write it cleaner.

    $dl = "D_GroupName"
    
    $members = Get-ADGroupMember $dl
    foreach ($member in $members){
    Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
    }
    
    • #62125
      Profile photo of brian catlin
      brian catlin
      Participant

      I got the first one you gave me to work perfectly the second one I was looking at but it gave me an error:

      $dl = "D_GroupName"
      
      $members = Get-ADGroupMember $dl
      foreach ($member in $members){
      Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
      }
      
      Get-ADUser : Invalid type 'Microsoft.ActiveDirectory.Management.ADPrincipal'.
      Parameter name: distinguishedName
      At line:5 char:1
      + Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Sel ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
          + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
       

      I was thinking maybe the word DistinguishedName was spelled incorrectly but I looked a user up under Attribute Editor in the Active directory and that looks to be the correct Attribute name and spelling.

      I found this post that said something about a bug its very close to the same error but not sure if that is related or not but figured I would share the website.

  • #62872
    Profile photo of Aapeli Hietikko
    Aapeli Hietikko
    Participant

    un-tested as I don't have domain to play with, but this little tweak should do the trick. I'd say that using
    $member.DistinguishedName would not work inside the filter as ad module is a bummer.

    $dl = "D_GroupName"
    
    $members = Get-ADGroupMember $dl
    foreach ($member in $members){
    $dn = $member.DistinguishedName
    Get-ADUser -Filter {DistinguishedName -like $dn} -Properties name,mail | Select name,mail
    }
    

    It could also work like this:

    $dl = "D_GroupName"
    
    $members = Get-ADGroupMember $dl
    foreach ($member in $members.DistinguishedName){
    Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
    }
    

You must be logged in to reply to this topic.