Automatically populate hash list based on input

I’m feeding the output from AD-GetUser into a hash list along the lines of:

$activeDirectory = @{}
Get-ADUser -Properties objectGUID,displayName,name,… |
ForEach-Object { $activeDirectory[$.objectGUID] = @{
distinguishedName = $
.distinguishedName
name = $_.name

}
}

Is there a way to change the “distinguishedName = $.distinguishedName" (etc.) part of the code so that it builds dynamically based on the available values from the input? It would be nice if I added a property to the Get-ADUser list that I don’t have to add a new "property = $.property” entry.

Related but slightly different, I have:

$groups = @()
$activeDirectory.GetEnumerator() | ForEach-Object {
if ($($.Value.objectClass) -eq “group”) {
$groups += [pscustomobject]@{
objectGUID = $
.Key
distinguishedName = $.Value.distinguishedName
displayName = $
.Value.displayName

}
}
}

Again, is there a way to automate the value-processing part?

Thanks.

pj48363,
Welcome back to the forum. :wave:t4: … long time no see. :wink:

Before we proceed … could you please format your code as code?

To post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink: