Richard
Richard

Reputation: 335

Powershell get-adgroupmembership text file

Currently i have:

import-module activedirectory
$path = get-content "E:\test.txt" | Out-String
$path | ForEach-Object { Get-ADGroupMember $_ | Select-Object name }

This gets the names (lastname_firstInitial) of the users within the group specified in the text file. The text file looks somthing like:

groupname1
groupname2
...
groupname50

is there a way to output the full name of the user "displayname" property, get-adgroupmember does not support the -property displayname or firstname & last name - i also need the names to appear next to the correct group they were pulled from.

Currently i can only retrieve the "logon names" but have no idea what group they were pulled from.

Thank you.

Phil

Upvotes: 1

Views: 5459

Answers (1)

Michael
Michael

Reputation: 672

When issuing this command you get a list of all members, each containing a unique ID in the AD. You could use this to fetch the actual ADUserObject and work your way from there.

$Filename = "C:\temp\groups.txt"

#ForEach ( $GroupName in [System.IO.File]::ReadLines($Filename) ) {
 ForEach ( $GroupName in (Get-Content $Filename) ) {
     $UserIDs = (Get-ADGroupMember $GroupName).ObjectGUID

     ForEach ( $UserID in $UserIDs ) {
          $ADUser = Get-ADUser $UserID -Properties DisplayName
          Write-Output "$GroupName : $($ADUser.DisplayName)"
    }
}

Upvotes: 2

Related Questions