PowerShell: Add New Value to Array

Viewed 27

Using PowerShell I retrieve information from multiple ActiveDirectory groups and output it to a single CSV file. To each row I want to add the name of the group, in addition to the information retrieved. How can I achieve this?

Desired Result:

"CN","Department","co","Company","Group"
"ABCDEF","Media","UK","Global Entertainment","XXX"

Current Result:

"CN","Department","co","Company"
"ABCDEF","Media","UK","Global Entertainment"

My PowerShell code:

#Roles to check membership for
$Roles= @(
'XXX' 
'YYY'
)

$FolderName = "C:\"
$OutputAD = @()

foreach ($role in $Roles) {
  $GroupMembers = Get-ADGroupMember -identity $role | select name

  $GroupMembersArray = $GroupMembers | Foreach {"$($_.Name)"}

  $GroupMembersArray | ForEach-Object {
    $UserLookup = Get-ADUser -Filter "Name -eq '$_'" -Properties CN,Department,co,Company | Select-Object CN,Department,co,Company
    $OutputAD +=  $UserLookup
  } 

}

$OutputAD | Export-Csv "$FolderName\Membership $(get-date -Format yyyy-MMM-dd-HH-mm-ss).csv" -NoTypeInformation
1 Answers
Related