How to Set Variables in PowerShell?

In PowerShell, you can create a variable by defining a variable name and then setting its value with the Set-Variable command. In this tutorial, I will explain how to set variables in PowerShell using different methods and with examples.

To set a variable in PowerShell using the Set-Variable cmdlet, use the following syntax: Set-Variable -Name "VariableName" -Value "Value". For example, to set a variable named “State” with the value “California”, you would write: Set-Variable -Name "State" -Value "California". You can then access the variable by typing $State, which will output “California”.

Set Variables in PowerShell

A variable in PowerShell is a storage location that holds data that can be used and manipulated throughout a script. Variables can store many data types, including strings, integers, arrays, and complex objects.

There are different methods to set a variable in PowerShell.

Set Variables with the Set-Variable Cmdlet

The best way to set a variable in PowerShell is by using the Set-Variable cmdlet. This cmdlet assigns a value to a specified variable or changes the current value. If the variable does not exist, the cmdlet creates it. Here’s an example:

Set-Variable -Name "State" -Value "California"

In this example, we set a variable named “State” with the value “California”. You can then access the value of the variable by typing the variable name preceded by a dollar sign ($), like this:

Set-Variable -Name "State" -Value "California"$State
$State

Output:

California

I executed the above PowerShell command, and you can see the exact output in the screenshot below:

Set Variables in PowerShell

Check out PowerShell Global Variables

Set Variables with Direct Assignment

Another way to set variables in PowerShell is by using direct assignment. This method is more concise and commonly used. To create and set a variable in PowerShell, you use the $ symbol followed by the variable name and an assignment operator (=).

Here’s an example:

$City = "New York"

In this case, we set a variable named “City” with the value “New York”. You can access the value of the variable in the same way as before:

$City = "New York"
$City

Output:

New York

Here is the exact output you can see in the screenshot below:

powershell set variable

Check out PowerShell Local Variables

Set Multiple Variables at Once in PowerShell

PowerShell allows you to set multiple variables at once using a single command. This can be done by separating the variable assignments with a semicolon (;). Here’s an example:

$FirstName = "John"; $LastName = "Doe"; $Age = 30

In this example, we set three variables: “FirstName” with the value “John”, “LastName” with the value “Doe”, and “Age” with the value 30.

This is how to set multiple variables in PowerShell.

In this tutorial, I have explained different methods to set variables in PowerShell, including using the Set-Variable cmdlet and direct assignment. We also explored how to set multiple variables at once in PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.