Home » PowerShell » PowerShell Variables

PowerShell Variables

PowerShell variables are used to store, manipulate and share data in the scripts. Variables are essential components of PowerShell scripts and utilizing them properly in scripts enhances the script efficiency, readability, and maintainability.

Creating a variable in PowerShell is very simple, use the dollar sign ($) followed by a name to declare a variable.

In this article, we will discuss the PowerShell variable, its usage, and how to use them in scripts.

Creating And Assigning Variables in PowerShell

Use the ($) before the name to create a variable in PowerShell. To assign a value to a variable use the equal sign (=).

$userName = "ShellAdmin"

In the above PowerShell script, the $userName is a variable name and it holds the value “ShellAdmin“.

PowerShell Variable Types

PowerShell is a dynamically typed language. The type of a variable is determined at runtime based on the value it holds.

The common variable types include integers, strings, floating-point, arrays, objects, and more. You can also explicitly specify the type of a variable using a type constraint:

[int]$age = 24

Using Variables in Strings

You can use the PowerShell variable in strings, using the following script:

$userName = "Shell Admin"
$greeting = "Hello! $userName"
Write-Host $greeting

In the above PowerShell script, the variable $username is used in the string and the $greeting variable is used in the command.

The output of the above script prints the greeting:

Hello! Shell Admin

It displays the output with the value of $userName as the entire string is double-quoted, it’s a variable expansion of a variable.

Working with Special Characters in Variable Names

PowerShell variable does not allow special characters like dashes, spaces, or colons in them. In some cases, if you want to use the special characters in variable names then enclose the variable name in curly braces {} and precede it with a dollar sign $ :

${test-user} = "Shell Admin"

In the above PowerShell script, the ${test-user} is a variable that contains a special character dash in between them.

Comparing PowerShell Variables and Environment Variables

PowerShell variables are used to store the values while environment variables store system settings, and user profile information. You can access the environment variables in PowerShell using the Env: drive:

$env:Path

PowerShell Variable Naming Convention

It’s important to follow the naming conventions for variables. It helps your script’s readability and maintainability.

Use descriptive names and follow the camelCase or PascalCase convention:

# CamelCase naming
$firstName = "Tom"

# PascalCase naming convention
$LastName = "Jackson"

Type Conversion of Variable

While working on the script, we need to convert variables from one type to another, you can do this using type casting:

$number = "23"
[int]$age = $number

In the above PowerShell script, the $number variable holds the string type value. To convert the variable type to an integer type value, we use type casting by specifying the [int] before the variable name.

Scope of Variables

Variables can be global, local, script, or private. Variable in PowerShell has a specific scope depending on where they are defined.

The Set-Variable cmdlet in PowerShell is used to specify the variable scope.

Set-Variable -Name "piValue" -Value "3.145" -Scope Global

In the above PowerShell script, the Set-Variable command declares a variable $piValue with an initial value of 3.145 and Global scope.

Conclusion

I hope the above article about the PowerShell variables is helpful to you. Variables are an essential part of the script and understanding it can help to improve efficiency and write a robust script.