Author Posts

August 30, 2016 at 3:38 pm

I have the following:

$Groups = Get-ADGroup -Filter {Name -like "LA*"}
foreach ($group in $groups) 
    {
    get-adgroupmember $group.name| where {$_.objectclass -eq 'Group'} | ft $group.name, name
    }

I'm just trying to cleanup the output, I would prefer to see:

$group.name Name
$group.name Name
$group.name Name
$group.name Name

vs. what I'm getting:

LA-STGGENAPP01 name
————– —-
{} DP-IT-Admin-DBA-NonProd

LA-STGGLLAND02 name
————– —-
{} DP-IT-Admin-DBA

LA-STGRPT01 name
———– —-
{} DP-IT-Admin-DBA-NonProd

LA-STGSSAS01 name
———— —-
{} DP-IT-Admin-DBA-NonProd

Still learning powershell so be nice 🙂

August 30, 2016 at 3:49 pm

So, your problem is here:

ft $group.name, name

This is telling Format-Table to create one column **named whatever is in $group.name**. The -Properties parameter of Format-Table is a list of columns you want displayed, not the **contents of those columns**.

You might try something like:

Format-Table @{n='GroupName';e={ $group.name }},Name

And see if that's more to your liking?

August 30, 2016 at 3:56 pm

AWESOME! Thanks @DonJones for the nice explanation and that created exactly what i was looking for. Is there a way when doing the foreach to have it output everything into a single table, output currently:

GroupName       Name                   
---------       ----                   
LA-TST3GENAPP01 DP-IT-Admin-DBA-NonProd



GroupName      Name                             
---------      ----                             
LA-TSTCRDAPP06 DP-TradeOpsTeam-Developer-NonProd



GroupName      Name                   
---------      ----                   
LA-TSTGENAPP01 DP-IT-Admin-DBA-NonProd



GroupName   Name                   
---------   ----                   
LA-TSTRPT01 DP-IT-Admin-DBA-NonProd



GroupName    Name                   
---------    ----                   
LA-TSTSSAS01 DP-IT-Admin-DBA-NonProd

vs. say:


GroupName    Name                   
---------    ----                   
LA-TSTSSAS01 DP-IT-Admin-DBA-NonProd
LA-TSTRPT01 DP-IT-Admin-DBA-NonProd

August 30, 2016 at 4:02 pm

Change the Format-Table to Select-Object and try it. The problem now is that you're creating a new table "foreach group in groups," right? It'll default to table format anyway since it's only two properties.

August 30, 2016 at 4:06 pm

Thats perfect, just as you described:

GroupName      Name                   
---------      ----                   
LA-STGGENAPP01 DP-IT-Admin-DBA-NonProd
LA-STGGLLAND02 DP-IT-Admin-DBA        
LA-STGRPT01    DP-IT-Admin-DBA-NonProd
LA-STGSSAS01   DP-IT-Admin-DBA-NonProd
LA-TST2GENA... DP-IT-Admin-DBA-NonProd
LA-TST3GENA... DP-IT-Admin-DBA-NonProd
LA-TSTCRDAPP06 DP-TradeOpsTeam-Deve...
LA-TSTGENAPP01 DP-IT-Admin-DBA-NonProd
LA-TSTRPT01    DP-IT-Admin-DBA-NonProd
LA-TSTSSAS01   DP-IT-Admin-DBA-NonProd