# Multiple variables challenge

**URL:** <https://forums.powershell.org/t/multiple-variables-challenge/15011>\
**Category:** PowerShell Help\
**Created:** [September 26, 2020, 11:45pm UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011 "2020-09-26T23:45:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [September 26, 2020, 11:45pm UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/1 "2020-09-26T23:45:56Z")

</div>

I will try to ask my question via sample code

```
$TeamsSettings = get-teams #I can run this line only once for some specific reasons
Function one{
foreach ($team in $TeamsSettings ){
$teamid= $team.Id
$settings = get-settings | {$_.teamid - $teamid} # this can be run only once in a script
new-variable -Name "settings${teamid}" -Value $settings -Scope script #I need to use this variable in second function
#e.g for this team, settings are stored in variable $settings3838
}}

Function two{
foreach ($team in $TeamsSettings ){
$teamid= $team.Id #I use this to determine second part of the variable name from first function to get right value for this team
 $settings${teamid} # I need to get variable value from first function (e.g. $settings3838) for each team but I'm not sure how
}}
```

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [September 27, 2020, 12:32am UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/2 "2020-09-27T00:32:30Z")

</div>

Use a PS custom object:

```
$TeamsSettings = get-teams

$TeamInfo = foreach ($team in $TeamsSettings){
    New-Object -TypeName PSObject -Property ([Oredered]@{
        TeamId = $team.Id
        Settings = get-settings | where { $_.teamid -eq $team.id } # this can be run only once in a script
    })
}

foreach ($team in $TeamsSettings){
    $thisteam = $TeamInfo | where { $_.TeamId -eq $team.id } # use this to determine the team based on its id
    $thisteam.Settings # Use the settings.. 
}
```

Or better yet, I would pick the desired properties from each of the 2 input objects (the one from get-teams and the one from get-settings), and create a PS custom object that is to be used for the rest of the script. Joining the 2 objects would be by the ‘id’ property as in:

```
$TeamDesiredProperyList = @('bla1','bla2','bla3')
$SettingsDesiredProperyList = @('bla4','bla5','bla6')

$myBlendedTeamObjectList = foreach ($Team in (get-teams | select $TeamDesiredProperyList)){
    $Settings = get-settings -TeamId $Team.Id | select $SettingsDesiredProperyList
    # Assuming the get-settings cmdlet/function has a TeamId parameter, otherwise use: 
    # $Settings = get-settings | where { $_.teamid -eq $team.id } | select $SettingsDesiredProperyList

    # Now build your PS object to use for the script:
    $Temp = New-Object -TypeName PSObject -Property @{ TeamId = $team.Id }
    foreach ($Property in $TeamDesiredProperyList) {
        $Temp | Add-Member -MemberType NoteProperty -Name $Property -Value $Team.$Property
    }
    foreach ($Property in $SettingsDesiredProperyList) {
        $Temp | Add-Member -MemberType NoteProperty -Name $Property -Value $Settings.$Property
    }
    $Temp
}
```

This is coded such that if you would like to add/remove properties from your custom object, you only need to change the $TeamDesiredProperyList and/or $SettingsDesiredProperyList arrays…

---

<div class="post-metadata">

**Author:** ![sasaz](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sasaz/32/1821_2.png) [@sasaz](https://forums.powershell.org/u/sasaz)\
**Post date:** [September 27, 2020, 3:50am UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/3 "2020-09-27T03:50:50Z")

</div>

That should be straightforward. In second function use get-variable

```
Function two{ foreach ($team in $TeamsSettings ){ 
$teamid= $team.Id # 
$settings = (get-variable -name "settings$($teamid)").value 
}}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [September 27, 2020, 7:46am UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/4 "2020-09-27T07:46:12Z")

</div>

Oh, I tried various things but get-variable. Works great. Thanks Sasa 🙂

---

<div class="post-metadata">

**Author:** ![adrianj](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@adrianj](https://forums.powershell.org/u/adrianj)\
**Post date:** [September 29, 2020, 11:20am UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/5 "2020-09-29T11:20:56Z")

</div>

Thank you Sam for your help as well. I was using functions so I needed to get variable outside of the function that are dynamically created and Sasa’s solutions worked great for me. Thanks again

---

<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:28pm UTC](https://forums.powershell.org/t/multiple-variables-challenge/15011/6 "2024-05-16T20:28:47Z")

</div>

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


