# Automatically populate hash list based on input

**URL:** <https://forums.powershell.org/t/automatically-populate-hash-list-based-on-input/21019>\
**Category:** PowerShell Help\
**Created:** [November 26, 2022, 11:22pm UTC](https://forums.powershell.org/t/automatically-populate-hash-list-based-on-input/21019 "2022-11-26T23:22:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pj483](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@pj483](https://forums.powershell.org/u/pj483)\
**Post date:** [November 26, 2022, 11:22pm UTC](https://forums.powershell.org/t/automatically-populate-hash-list-based-on-input/21019/1 "2022-11-26T23:22:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 27, 2022, 9:16pm UTC](https://forums.powershell.org/t/automatically-populate-hash-list-based-on-input/21019/2 "2022-11-27T21:16:07Z")

</div>

pj48363,  
Welcome back to the forum. 👋🏽 … long time no see. 😉

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](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/automatically-populate-hash-list-based-on-input/21019/3 "2024-05-16T20:16:22Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


