Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

List Active Directory Group Members and export them to a csv file.

$
0
0

Hi,

Iam using the script made by Anthony Guimelli.

I have one problem, if a users is in more than 1 one Group the output is not correct.

The output would be like this:

user1;groupname1;mobile;XXX;XXX;
user1;groupname2;mobile;XXX;XXX;

I want the output to be like this:

user1;groupname1;groupname2;mobile;XXX;XXX;

Here is a sample script based on Anthonys:

Import-Module ActiveDirectory

# group list separated by commas
$groupArray = "groupname1","groupname2"


# path to output file
$csvFilePath = "c:\temp\test$((Get-Date).ToString("yyyyMMddHHmmss`")).csv"

# First row in CSV
$csvHeaderRow = "Test;"

# Writes the first line to the CSV file
$csvHeaderRow | Out-File $csvFilePath -Encoding "Default"

# Iterates through the array of group name strings and instantiates DirectoryEntry objects off of them
# in order to obtain values to write to the output file.
foreach ($groupName In $groupArray) {
  $groupObject = Get-ADGroup -Identity $groupName -Properties Member,cn
  $groupname1 = $groupObject.cn

  $memberCollection = $groupObject.Member
  foreach ($distinguishedName In $memberCollection){   
   $userObject = Get-ADObject -Identity $distinguishedName -Properties cn,sn,givenName,mail,mobile
   $userName = $userObject.cn
   $firstName = $userObject.givenName
   $lastName = $userObject.sn
   $mailDN = $userObject.mail

    
   $csvRow = "$userName;$firstName;$lastName;$maildn;$groupname1;groupname2;"
   $csvRow | Out-File $csvFilePath -Append -Encoding "Default"       
  }    
}

#--------------------------------------------------------------------------------

How do i fill in the "groupname2" field in the output in the best way.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>