top of page
  • Writer's pictureshishir kushawaha

Take control of your data with the power of PowerShell variables

Updated: Mar 30

A PowerShell variable is essentially a named container in memory that holds a value, which can be changed or accessed later in the program. The name of the variable acts as a reference to its stored value, so you can use the variable name throughout your code to access the value it holds. This allows you to manipulate data within your program and makes it much easier to write complex scripts and programs.

Variables in PowerShell can store different data types, such as string, integer, float, and arrays. Variables in PowerShell are declared using the $ symbol followed by the variable name. For example, to declare a string variable named "message", you can use the following code:

$message = "Hello, World!"

In PowerShell, there are several ways to create a variable and assign a value to it:

  • Simple assignment: You can create a variable and assign a value to it in a single step, using the assignment operator =, like this:

    $message = "Hello, World!"
    Write-Output $message
  • Declare and assign: You can first declare the variable and then assign a value to it, like this:

    $message = "Hello, World!"
    Write-Output $message
  • Read from the pipeline: You can create a variable and assign a value to it by reading from the pipeline, like below:

    Write-Output "Hello, World!" | Set-Variable -Name message
    Write-Output $message
  • Read from user input: You can create a variable and assign a value to it by reading input from the user, like this:

    $message = Read-Host "Enter a message"
    Write-Output $message

These are some of the ways to create a variable and assign a value to it in PowerShell. The method you choose will depend on your specific use case and the type of data you are working with.

Here are some of the most commonly used built-in cmdlets in PowerShell for working with variables:

  • Get-Variable: This cmdlet is used to retrieve the values of variables in the current scope. For example, to get the value of a variable named $message, you can run the following command:

    Get-Variable -Name message
  • Set-Variable: This cmdlet is used to create or update a variable with a new value. For example, to create a variable named $message with a value of "Hello, World!", you can run the following command:

    Set-Variable -Name message -Value "Hello, World!"
  • New-Variable: This cmdlet is similar to Set-Variable, but it allows you to create a variable with a specific data type. For example, to create an integer variable named $age with a value of 25, you can run the following command:

    New-Variable -Name age -Value 25 -Option ReadOnly -Force
  • Remove-Variable: This cmdlet is used to remove a variable from the current scope. For example, to remove a variable named $message, you can run the following command:

    Remove-Variable -Name message -Force
  • Clear-Variable: This cmdlet is used to clear the value of a variable, while leaving the variable itself in place. For example, to clear the value of a variable named $message, you can run the following command:

    Clear-Variable -Name message -Force

With these cmdlets, you can easily create, manage, and manipulate variables in your scripts and commands.

Several types of variables that you can use, including:

  • Scalar Variables: These are single values that can store a single string, integer, or other simple data type. For example:

    $name = "John Doe"
    $age = 35
  • Array Variables: These are collections of values, stored in a list. You can access individual values in an array using indexing. For example:

    $fruits = "Apple", "Banana", "Cherry"
    Write-Output $fruits[0]
  • Hashtable Variables: These are collections of key-value pairs, similar to dictionaries in other programming languages. You can access values in a hashtable using the keys. For example:

    $person = @{
        Name = "John Doe"
        Age = 35
        City = "New York"
    Write-Output $person["Name"]
  • Object Variables: These are complex data structures that store properties and methods of an object. You can access properties and methods of an object using the dot notation. For example:

    $date = Get-Date
    Write-Output $date.Day
  • Automatic Variables: These are special variables in PowerShell that hold important information, such as the current directory, the current date and time, and the current user name, among others. These variables are automatically created and updated by PowerShell, and you can access them using their names. For example:

    Write-Output $PWD
    Write-Output $PSVersionTable
  • Dynamic Variables: These are variables that can be declared and used dynamically, without specifying the type of the variable. You can use dynamic variables to store values of different types in the same variable, but this can lead to unexpected behavior in some cases. For example:

    $dynamic = "Hello, World!"
    $dynamic = 42
    Write-Output $dynamic

Things to note during variable declaration and value assignment in PowerShell:

  • Variable names are case-insensitive. It can only contain letters, numbers, and underscores, and must start with a letter or underscore. Avoid using names that are reserved for PowerShell cmdlets and parameters.

  • By convention, PowerShell variable names should not contain spaces or special characters, except for the underscore (_) character. However, if you wish to use, then enclose variable name in curly braces {} like below:

		${my variable}="123"
		${my variable}
  • Variables can be declared in different scopes, such as the global scope, the script scope, or the function scope. Variables declared in a lower scope are only accessible in that scope and its child scopes.

  • When assigning string values to variables, you can use either single quotes or double quotes. Single quotes preserve the literal value of the string, while double quotes allow for variable expansion and escape sequences.

  • You can access the value of a variable using the syntax $VariableName. You can also use the syntax ${VariableName} if the variable name is followed by a character that would otherwise be interpreted as part of the variable name.

		Get-ChildItem ${env:ProgramFiles(x86)}
  • Variable lifecycle: Variables are automatically destroyed when they go out of scope. However, you can also explicitly unset a variable using the Remove-Variable cmdlet.

  • Variables in PowerShell can be assigned a null value using the $null variable, or an empty value using the $empty variable. These variables are different from each other, and from an empty string, which is represented by "".

In conclusion, variables are an essential component of PowerShell, providing a way to store and manipulate data in your scripts and commands. There are several different types of variables, including scalar variables, array variables, hashtables, and environment variables, and you can use different methods to create and assign values to these variables. It is important to understand the different ways in which variables can be used and manipulated, and to keep in mind the key features and quirks of working with variables in PowerShell. By mastering these concepts, you can take full advantage of the power and flexibility of this powerful scripting language.

623 views0 comments
bottom of page