Falco
Falco

Reputation: 111

PowerShell: Add New Value to Array

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

Upvotes: 1

Views: 342

Answers (1)

Falco
Falco

Reputation: 111

Found the solution. In Select-Object you can add a calculated property.

Example: Select-Object CN,Department,co,Company,@{n='Role';e={$role}}

Upvotes: 2

Related Questions