Powershell: dynamic variables within foreach


I am trying to do some math for a dynamic list of users that I will use to run a search and count some results anonymously, then add to a running total per user. So, for instance:

$users = Get-ADUser -filter blah
[int]$usercount = $users.count

for ($z=1; $z -le $usercount; $z++) {
    **** create variable here - $user$z ***

When the variable is created, I need it available for further loops where I will add a count to the number already stored in the variable.

And no, I can't use the $user variable, because it must persist after this foreach loop ends.

So, the question is, how to I generate that incrementing variable not knowing the limit of the count of possible objects?


Adding an easy example of what I am talking about...

After feedback, I am looking at hashtables, but still can't figure out how to reference.

Imagine a dice game between a dynamic list of people with multiple rounds. I want to increment per round their total. My problem is the last line where I try to update the total with the roll. How do I reference the hashtable value?


param (
    [parameter(Mandatory=$false)][ValidateRange(1, [int32]::MaxValue)][int]$rounds = "15",
    [parameter(Mandatory=$false)][ValidateRange(1, [int32]::MaxValue)][int]$players = "2"

$ptotal = [ordered]@{}
for ($w=1; $w -le $players; $w++) {
    $ptotal.add("player$w", 0)

for ($z=1; $z -le $rounds; $z++) {
    Write-Host Round $z

    for ($y=1; $y -le $players; $y++) {

        $roll = (1..6 | get-random) + (1..6 | get-random)
        $ptotal.player$y = $ptotal.player$y + $roll

in your example it would be $ptotal["player$y"] += $roll

You can use Hashtables both like associative arrays and objects. Very good reading about them is https://kevinmarquette.github.io/2016-11-06-powershell-hashtable-everything-you-wanted-to-know-about/ (on top of official documentation, of course)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


PowerShell foreach within function

Concatenating Variables within Powershell

Updating variables within variables in Powershell

ForEach with Multiple Variables - Powershell

Powershell - Variables and foreach loop

Using "dynamic variables" within a function

Creating dynamic variables for a class from within the class

PHP Foreach variables to dynamic javascript/jquery

Pass variables to Foreach-Object Powershell 7

Event binding on dynamic HTML, using fetch and within forEach

Filtering dataframe rows from dynamic variables within shiny

Javascript Set Dynamic Variables inside a For Loop Within addEventListener

Setting dynamic session variables(arrays) with a prefix and looping with foreach

variables within controller foreach returns NULL inside view

Azure ARM deployment, passing dynamic variables from powershell

Returning multiple variables from within method in powershell class

Prepending text to only certain variables within a Powershell function

Reading XML data using Powershell foreach and assign to the values Variables

Increment variable in PowerShell from within if statement within a foreach loop. Seems broken

Terraform variables within variables

bash variables within variables

How to declare variables within dynamic columns in SQL Server 2005 using PIVOT

R: Non-standard evaluation of dynamic variables within pipes. Replacing (") with (`)

Using foreach within a foreach with php

Foreach loops within a foreach loop

Dynamic Hyperlink within an IF function

Replacing dynamic variables using REGEX content as variable in Azure CD Pipeline Powershell Task

Chat system foreach dynamic

Dynamic expresso foreach with lambda?

TOP Ranking