Author Posts

October 7, 2017 at 4:16 pm

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.

October 7, 2017 at 9:30 pm

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.

October 8, 2017 at 11:22 pm

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.