Variables

We will learn about variables and their rules in Powershell and compare them with Python.

Variables

A variable is a unit of memory, typically used to store values like results from commands, expressions, paths, strings, settings, etc.

There is no need to explicitly define data types during the variable declaration in Powershell and Python, because both are dynamically typed programming languages. That means the data type of a variable is interpreted during the run time, depending upon the value the variable is holding.

Rules for naming PowerShell variables

The following rules must be observed when declaring a variable in the Python language:

  • PowerShell variable names are represented by text that begins with a dollar sign ( $ ).

  • Variable names are case-insensitive, which means $var, $Var and $VAR represent the same unit of memory to store data.

  • Variable names can include spaces and special characters, but these are difficult to use and are not very intuitive so can generally be avoided.

    PS C:\> ${this is a variable} = 12
    PS C:\> ${this is a variable}
    12
    

Run the following script to see its output:

Get hands-on with 1400+ tech skills courses.