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
  • 1
    There's no need to declare the var beforehand in PS. But you should also research variable scope, which is going to cause problems with the way you're going right now. – music2myear Nov 7 '18 at 0:18
  • I actually do need to declare beforehand, because I will have multiple loops later that will add, so I cannot just use a variable within the loop. Also understand the scope concern, and I will deal with that, I am just looking for how to create the variable in a way that I can read it later... – Steven Nov 7 '18 at 0:25
  • So long as you continue to reference the vars using $Script:var_name you can manipulate them throughout the script, in any loop or function. If this is not what you are asking, perhaps you can clarify? – music2myear Nov 7 '18 at 0:35
  • 2
    You can create variables with New-Variable cmdlet. As for loop does not introduce new scope, them will be available in following loops in current scope by default. You can specify desired scope as parameter to New-Variable cmdlet as well. But, unless you can provide good justification for using variables with dynamic names, you should consider using array instead. – user364455 Nov 7 '18 at 0:46

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)

  • @Steven you are welcome. You can use "Vote up" as an expression of gratitude ;) – maoizm Nov 7 '18 at 15:35
  • 1
    @maoizm "Vote up" require 15 points, but OP only have 3. – user364455 Nov 7 '18 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.