locked
Format-Table output variable RRS feed

  • Question

  • How do you display the value of $group in Format-Table?  Currently the code below display the value as a label instead of a value in each row.

    function get-members {
    
    Get-ADGroupMember -Recursive $group | Format-Table $group, Name, samAccountName, Title, Department
    }
    
    $groups = (Get-Content E:\psscript\ADgroups.txt)
    
    
    foreach ($group in $groups){
    get-members
    }

    Thursday, October 26, 2017 9:01 PM

Answers

  • Get-Content E:\psscript\ADgroups.txt |
    	ForEach-Object{
    		$groupname = $_
    		Get-ADGroupMember $groupname -Recursive  |
    			Select @{n='GroupName';e={$groupname}},*
    	} |
    	Format-Table GroupName, Name, samAccountName, Title, Department

    Help Format-Table -online
    help Select-Object -online
    help ForEach-Object -online


    \_(ツ)_/



    • Edited by jrv Friday, October 27, 2017 12:15 AM
    • Marked as answer by willtwc Friday, October 27, 2017 4:05 PM
    Thursday, October 26, 2017 9:20 PM

All replies

  • Get-Content E:\psscript\ADgroups.txt |
    	ForEach-Object{
    		$groupname = $_
    		Get-ADGroupMember $groupname -Recursive  |
    			Select @{n='GroupName';e={$groupname}},*
    	} |
    	Format-Table GroupName, Name, samAccountName, Title, Department

    Help Format-Table -online
    help Select-Object -online
    help ForEach-Object -online


    \_(ツ)_/



    • Edited by jrv Friday, October 27, 2017 12:15 AM
    • Marked as answer by willtwc Friday, October 27, 2017 4:05 PM
    Thursday, October 26, 2017 9:20 PM
  • The result is the same as my script. The value of the Groupname column is empty.

    GroupName Name                samAccountName      Title Department
    --------- ----                --------------      ----- ----------
    {}       


    Thursday, October 26, 2017 9:54 PM
  • Your file must be broken.  What is in the file?

    Run this and see what you get.

    Get-Content E:\psscript\ADgroups.txt |
    	ForEach-Object{
    		Write-Host $_
    	}


    \_(ツ)_/

    Thursday, October 26, 2017 10:00 PM
  • Both scripts return members of all the AD group defined in the text file. But for each row it does not display the AD group name.  All columns are populated except the groupname.

    Thursday, October 26, 2017 10:19 PM
  • Post  the exact code you used.


    \_(ツ)_/

    Thursday, October 26, 2017 10:32 PM
  • Sorry.  The pipe got left out somehow.  Copy the example again and it will work.


    \_(ツ)_/

    Thursday, October 26, 2017 10:33 PM
  • There is no value returned under the Groupname column.  But the curly brackets are gone.

    GroupName name           SamAccountName Title Department
    --------- ----           -------------- ----- ----------
              xxx            xxxx           xxx   xxxx
         

    Thursday, October 26, 2017 11:30 PM
  • Post the exact code you are using to get that.


    \_(ツ)_/

    Thursday, October 26, 2017 11:32 PM
  • Finally this is how to add the missing user properties:

    Get-Content E:\psscript\ADgroups.txt | 
    	ForEach-Object{
    		$groupname = $_
    		Get-ADGroupMember $groupname -Recursive |
    			Where{$_.objectClass -eq 'User'} |
    			Get-ADUser -Properties Title, Department |
    			Select *,@{n='GroupName';e={$groupname}}
    	} |
    	Format-Table GroupName, Name, samAccountName, Title, Department

    This code is tested and works as described.  If you are having issues then you need to check how you are using the code.


    \_(ツ)_/

    Friday, October 27, 2017 12:21 AM
  • This code works now.   Not sure why it was not displaying the group name.

    Get-Content E:\psscript\ADgroups.txt |
    	ForEach-Object{
    		$groupname = $_
    		Get-ADGroupMember $groupname -Recursive  |
    			Select @{n='GroupName';e={$groupname}},*
    	} |
    	Format-Table GroupName, Name, samAccountName, Title, Department


    Friday, October 27, 2017 4:06 PM