Get-ADgroup, With Poershell want to display/export output One user per line

Welcome Forums General PowerShell Q&A Get-ADgroup, With Poershell want to display/export output One user per line

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

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #81514

    Participant
    Points: 0
    Rank: Member

    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.

  • #81532

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

    PowerShell's formatting system will produce a table when you give it less than 5 properties, by default. If you want something other than that, you're going to need to code it out yourself. You'll have to enumerate each one and output whatever you want.

    Output formatting is frankly not PowerShell's strongest superpower – if you want anything more than the default, you end up having to code it up yourself.

  • #81548

    Participant
    Points: 208
    Helping Hand
    Rank: Participant

    What about just doing something like the following...

    ### Show User and AD group membership

    # Get users with all their properties and their group membership, display user and group name
    ForEach ($TargetUser in (Get-ADUser -Filter * -Properties *))
    {
    "`n" + "-"*12 + " Showing group membership for " + $TargetUser.SamAccountName
    Get-ADPrincipalGroupMembership -Identity $TargetUser.SamAccountName | Select Name
    }

    # Get users with base properties and their group membership, display user and group name
    ForEach ($TargetUser in (Get-ADUser -Filter *))
    {
    "`n" + "-"*12 + " Showing group membership for " + $TargetUser.SamAccountName
    Get-ADPrincipalGroupMembership -Identity $TargetUser.SamAccountName | Select Name
    }

    # Get user and AD group info, display user and group name
    Get-ADUser -Filter "*" -SearchBase "CN=Users,DC=contoso,DC=com" -SearchScope OneLevel -Properties Name, MemberOf |
    Select-Object Name, @{Label="Memberof";expression={($_.memberof | Get-ADGroup | Select-Object -ExpandProperty Name) -Join ","}}

    Get-ADUser -Filter "*" -SearchBase "CN=users,DC=contoso,DC=com" -SearchScope OneLevel -Properties Name, MemberOf |
    Select-Object Name, @{Label="Memberof";expression={($_.memberof | Get-ADGroup | Select-Object -ExpandProperty Name) -Join ","}} | Format-List

    Of course format as needed.

The topic ‘Get-ADgroup, With Poershell want to display/export output One user per line’ is closed to new replies.