Quick tip: Dynamically create and use variables

Creating and using variables in PowerShell is simple. Unlike most other languages you don’t need to initialize a variable before use – you don’t even need to type them; PowerShell will attempt to set the type based on the value you give it. Because they are so simple to use, it’s easy to forget that we actually have a set of cmdlets for working with variables. (Get/Set/New/Clear/Remove-Variable) But that’s ok, they are seldom needed. But in this quick tip I will show you how to create and use variables dynamically, and then they come in handy indeed.

Consider the following code:

$prefixes = @(
    'p1',
    'p2',
    'p3',
    'p4'
)

We have an array of prefixes that we want to use to prefix some variables that we will be using in our script. Our variables should be named ‘p1_var‘, ‘p2_var‘ and so on.

Using the New-Variable cmdlet we could achieve this the following way:

$count = 0
foreach ($prefix in $prefixes) {
    $count++
    New-Variable -Name "$($prefix)_var" -Value $count -Force
}

I’m using a counter to be able to give each variable a unique value. You see that the New-Variable have a Name parameter as well as a Value parameter. And since the Name parameter takes a string, it’s quite easy to create a dynamic name using a loop on our prefixes array, and by using an expression in the string to dynamically build our variable name. Notice that I’m using the Force switch to make sure that the variables will be created even if a variable with the same name exists already. If you just want to update the value of a variable, you could use the Set-Variable cmdlet instead.

Likewise, if we want to use these variables in a dynamic way, consider the following example:

foreach ($prefix in $prefixes) {
    Write-Host "Variable '$((Get-Variable -Name "$($prefix)_var").Name)' have a value of $((Get-Variable -Name "$($prefix)_var").Value)"
}

Again we are iterating through our array of prefixes, but this time we are using the Get-Variable to get the name and the value of the variables, which we write to the host.

I’ll not be surprised if there is an even easier way to achieve the same thing in PowerShell, so if you know of a better approach, please let me know in the comments section below.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s