1

I want to declare variables with random numbers in the name, such as

$var123 = "A"
$var456 = "B"
$var789 = "C"

by using Get-Random for the number part.

What have I to write behind $var if $rnd = Get-Random? Or is there another way to declare such variables?

Update: The real use case is that I have trouble with Add-Type and hoped I could solve it with this workaround. But it still does not work as expected. It works for the very first time, but after changing the C# code, it still shows the old code.

Any suggestions? I am running PowerShell 7.2.1 on a Mac computer.

$rnd = Get-Random
$code =
@"
    using System;
    namespace HelloWorld {
        public class Program {
            public static int Main() {
                #return 123; # first try, working well
                return 456; # after changing, still returns 123
            }
        }
    }
"@

New-Variable -Name "var$rnd" -value $code
Add-Type -TypeDefinition (Get-Variable -Name "var$rnd" -ValueOnly) -Language CSharp 
Write-Host (Invoke-Expression "[HelloWorld.Program]::Main()")
Christoph S.
  • 585
  • 3
  • 16
  • 2
    You didn't write what you need this for. I can't really imagine a use case, but maybe that's just me. Another way would be a hashtable: `$ht = @{}; $ht.$rnd = 'A'` – zett42 Feb 06 '22 at 19:25
  • The usecase is using inline C# code with `Add-Type`. When I change the C# code, I can't run the script anymore. I have to close and re-open PowerShell first. In this way, I always use another variable and can change the code again and again – Christoph S. Feb 06 '22 at 19:33

1 Answers1

3

You would need to use New-Variable or Set-Variable if you want to dynamically create them with random names:

'A', 'B', 'C' | ForEach-Object {
    do {
        $varname = "var$(Get-Random -Minimum 100 -Maximum 1000)"
    } until($varname -notin (Get-Variable var*).Name)
    New-Variable -Name $varname -Value $_
}

Get-Variable var*

Worth noting that, using above method will make an infinite loop if you create 901 variables with it. -Minimum and -Maximum should be tweaked if more variables are needed.


Regarding your update, I believe you already have been following this Q&A, one of the answers provides this workaround which is what I believe you wanted to do:

$rnd = Get-Random
$code = @"
using System;
namespace HelloWorld$rnd {
   public class Program {
       public static int Main() {
           return 123;
           // return 456;
       }
   }
}
"@

Add-Type -TypeDefinition $code -Language CSharp
Invoke-Expression "[HelloWorld$rnd.Program]::Main()"
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • 1
    I did it in this way: ```$rnd = Get-Random; New-Variable -Name "var$rnd" -value "A"; Write-Host (Get-Variable -Name "var$rnd" -ValueOnly)``` - thx @SantiagoSquarzon – Christoph S. Feb 06 '22 at 19:27
  • 1
    @ChristophSchreiber my pleasure, happy to help. You might want to consider zett42's comment, using a hash table instead (should be more appropriate for this). I can update my answer if needed. – Santiago Squarzon Feb 06 '22 at 19:30
  • Please read my updated question – Christoph S. Feb 06 '22 at 19:59
  • 2
    @ChristophSchreiber see this Q&A https://stackoverflow.com/questions/3369662/can-you-remove-an-add-ed-type-in-powershell-again there are workarounds for this. i.e.: testing your code on a different thread – Santiago Squarzon Feb 06 '22 at 20:13
  • 1
    @ChristophSchreiber see my update, I believe this is what you wanted to do since the beginning – Santiago Squarzon Feb 06 '22 at 20:19
  • 1
    The answer of nergeia (maybe typo) did what I needed, thank you very much @SantiagoSquarzon – Christoph S. Feb 06 '22 at 20:20