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
user1;groupname2
I want the output to be like this:
user1;groupname1
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.