none
Get-ADGroupMember, Want to display/export output One user per line. RRS feed

  • Question

  • I have created a Powershell script which fetch security group Members of specific account from multiple remote servers. But, the output I am getting is annoying. Assume group member as Server_Admins, Server_Admins1 and so on, then I want output as below:

    Server_Admins

    user1

    user2

    user3

    user4

    Server_Admins1

    user5

    user6

    user7

    user8 

    But, I am getting output in below format:

    Members                                                              Group Name

    user1 user2 user3 user4                                        Server_Admin

    user5 user6 user7 user8                                        Server_Admin1


    Powershell Script:

    $secgroups = Get-Content C:\Users\user1\Desktop\test.txt


    $array = @()

    foreach($grp in $secgroups){

    $members = Get-ADGroupMember $grp | Select-Object name

    $obj = New-Object psobject -Property @{

    'Name' = [String]$members.name

    'Server' = [String]$grp

    }

    $array+= $obj$array|Select-Object Name,Server| Export-Csv C:\Users\user1\Desktop\result.csv

    Note: test.txt contains list of security groups.




    Saturday, October 7, 2017 3:40 PM

Answers

  • It would be better if you took the time to learn PowerShell instead of guessing from looking at code on the Internet.

    foreach ($grp in $secgroups) {	
    	Get-ADGroupMember $grp | 
    	ForEach-Object{
    		[pscustomobject]@{
    			Group = $grp
    			Name = $_.name
            }
        }
    }
    


    \_(ツ)_/

    • Marked as answer by Charu Krishan Sunday, October 8, 2017 3:30 PM
    Saturday, October 7, 2017 5:19 PM

All replies

  • It would be better if you took the time to learn PowerShell instead of guessing from looking at code on the Internet.

    foreach ($grp in $secgroups) {	
    	Get-ADGroupMember $grp | 
    	ForEach-Object{
    		[pscustomobject]@{
    			Group = $grp
    			Name = $_.name
            }
        }
    }
    


    \_(ツ)_/

    • Marked as answer by Charu Krishan Sunday, October 8, 2017 3:30 PM
    Saturday, October 7, 2017 5:19 PM
  • Hi jrv

    i test a Little with your example but I don't understand it.

    1. is pscustomobjet a array? Most example to define an Array are $Array = ....   https://ss64.com/ps/syntax-arrays.html

    2. why doesn't this work

    3. Can I Output the result without for next (remarked code)?

     thanks for answer
    $x = get-adgroupmember group_TEST	
    ForEach($y in $x)
        {
    		[myarray]@{
                xyz = $y.name
            }
        }
        #$myarray[0]
    
    
    #for ($i=0; $i -lt $myarray.length; $i++){
    #    Write-Host $myarray[$i]   
    #}


    Chris

    Sunday, October 8, 2017 7:44 AM
  • Because it is not PowerShell.  You need to start by learning PowerShell.  We cannot teach you.  That is not the purpose of this forum.

    Learn PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    \_(ツ)_/

    Sunday, October 8, 2017 8:28 AM
  • Hi Jrv,

    Thank You! For answer and advise you gave me. I am a newbie and hopefully by accept your advise I will learn PowerShell effectively. 

    Can you please advise me a portal/forum where I can clear my doubts, if any during my learning?

    CK

    Sunday, October 8, 2017 3:36 PM