locked
script to get ad group members RRS feed

  • Question

  • Hi,

      I am trying to get members from adgroup name -like ms. 

       $Title      = 'Get_Security_Group_Members'
    
        
        #  Log file time stamp:
        $LogTime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
        
    
        $LogFile = $psscriptroot + '\' +$title + '-' +$LogTime+".log"
        
        $SGs = 'App_MS*'
    
        $Groups = Get-ADGroup -filter 'Name -like $SGs' #| select Name
    
        
           Foreach ($Group in $Groups)
              { 
              
                 $SGMembers = Get-ADGroupMember -Identity "$Group"  | select name  
    
                Write-Output "$Group" | Out-file $LogFile -append 
    
                Write-Output "    $SGMembers" | Out-file $LogFile -append 
    
            }

    the output i am getting with CN=SG,OU    AND @{name= USERNAME), instead just security group name first and members underneath.

    Issue i am getting in "$Groups = Get-ADGroup -filter 'Name -like $SGs' #| select Name" line. If i commit | select Name, it gives me

    cn.


    THANKS FOR HELP in advance



    orion

    Tuesday, July 17, 2018 11:06 PM

Answers

  • Use the -expandproperty command when you are selecting objects

    Get-ADGroupMember -Identity "$Group"  | select -expandproperty name

    This should get you the property that you need from that query.

    • Marked as answer by Ultra9.99 Thursday, July 19, 2018 3:46 AM
    Wednesday, July 18, 2018 2:10 PM

All replies

  • Look in the Gallery for numerous scripts that report on group membership by group.


    \_(ツ)_/

    Tuesday, July 17, 2018 11:29 PM
  • Get-AdGroup -filter App_MS* -PipelineVariable g | 
        Get-AdGroupMember |
        select @{n='GroupName';e={$g.name}},* |
        Format-Table -GroupBy GroupName -Auto


    \_(ツ)_/

    Tuesday, July 17, 2018 11:34 PM
  • Use the -expandproperty command when you are selecting objects

    Get-ADGroupMember -Identity "$Group"  | select -expandproperty name

    This should get you the property that you need from that query.

    • Marked as answer by Ultra9.99 Thursday, July 19, 2018 3:46 AM
    Wednesday, July 18, 2018 2:10 PM