What is a PowerShell variable

In this tutorial, we will discuss what is a PowerShell variable, different types of variables, how to declare a variable.

Also, what are user created variables, automatic variables, preference variables, read-only variables in PowerShell.

We will also see how to set value, clear value, and delete a PowerShell variable.

What is PowerShell variable

In PowerShell, we create variables to store values that we can use inside the script.

We can store all types of values in a PowerShell variables like:

  • integers
  • strings
  • arrays
  • hash tables
  • And objects that represent processes, services, event logs, and computers etc.

PowerShell variables are “loosely typed,” because they are not limited to a particular type of object.

A single variable can contain a collection of different types of objects at the same time.

Technically some memory will be allocated to a variable, where the values will be stored.

We can use Windows PowerShell ISE  Or Visual Studio Code to run, debug, and test PowerShell cmdlets.

How to declare and use PowerShell variables

We can declare a variable with an initial character as “$” and the name of the PowerShell variable.

The name can be alphanumeric characters or even we can use an underscore (_) in a name.

Variable names are not case-sensitive.

Variable names can include spaces and special characters but we should avoid these kinds of variable names. Instead, we should use _ to create a human-readable name.


$address ="Bangalore" etc

If you have not assigned any value to the variable, then the default value for the PowerShell variable will be $null.



Now below way, we can use to preserve values:

Write-Host $TodayDate

The above statement will write today’s date in dd-mm-yyyy format into the host.

We can create a variable with special characters but enclose the variable name with braces.



Types of PowerShell Variables

There are three types of PowerShell variables.

User Created Variables in PowerShell

The above PowerShell variables known as user-created variables because these variables are created and maintained by the users.

The value of these PowerShell variables will exist until the window is open.

Automatic Variables in PowerShell

There are also other variables are there which are known as Automatic variables.

These variables are created by Powershell which store the state of Windows PowerShell.

Users cannot change the value of these variables.

Examples: $PSHome, $Error, $Event, $EventArgs, $Foreach, $False, $Home, $Host $input etc.

Preference Variables in PowerShell

There is also another type of variable are there which are known as Preference variables which store user preferences for PowerShell.

These variables are created by PowerShell having some default values but the values can be changed by the user.

Below are few Preference variables with default values.

$DebugPreference : SilentlyContinue
$ErrorActionPreference : Continue
$MaximumErrorCount : 256
$MaximumFunctionCount : 4096 etc.

PowerShell Set-Variable

We can easily set a value to a variable by using Set-Variable PowerShell cmdlets.

The Set-Variable cmdlet assigns a value to a specified variable or changes the current value.

If the variable does not exist, the Set-Variable cmdlet will create a variable on that name.


Set-Variable -Name "url" -Value "https://www.SharePointSky.com"

Similarly, If you want to get the value of the variable then you can use the below cmdlet.

Get-Variable -Name "url"

It will return the value as “https://www.SharePointSky.com”.

Read-Only PowerShell Variable

Sometimes you may need to create some variables which you want to make read-only, the value of the variable should not change.

We can use the same Set-Variable cmdlets to create a read-only variable.

Set-Variable -Name "url" -Value "https://www.SharePointSky.com" -Option ReadOnly

But the value of a PowerShell read only variable can be changed by using the Force parameter.

Set-Variable -Name "url" -Value "https://www.EnjoySharePoint.com" -Force

How to clear value from a variable

We can clear value from variables by using Clear-Variable cmdlet like below:

Clear-Variable -name $TodayDate

Or we can also clear value from a variable by assigning $null like below:

$TodayDate = $null

How to delete a PowerShell variable?

We can delete variables by using Remove-Variable cmdlets like below in PowerShell:

Remove-Variable -name TodayDate

You may like following tutorials:


In this tutorial, we learned what is a variable in PowerShell, how to declare, set value to a variable?

Also we checked, what are the different types of variables, read-only variables, how to set and clear value from a variable,

And how to delete a variable in PowerShell.