How To Effectively Use PowerShell Variables

Variables are objects that can store data. In PowerShell, a variable name starts with a $ character.

  • A variable can be given any name. It can contain a mix of letters, numbers, symbols, or spaces.
  • The variable names in PowerShell are case insensitive.
  • You can assign a value to a variable using the assignment (=) operator.
  • A variable is created by simply assigning a value to it.
  • For example, the below command creates a variable named $myName and assigns it a string value. The double quotes (” “) indicate that a string value is being assigned to the variable.
How To Use PowerShell Variables
PowerShell Variables

Variables can be categorized into :

  • User-defined variables – Variables declared by the user which is session-oriented.
  • Automatic Variables – Variables that store the state of the information in the PowerShell console.
  • Environment variables – Variables that hold the environment in which programs run.

How To Assign Values To PowerShell Variables

Highlights:Storing multiple objects in a variableDiscovering the variable’s type, properties and methodsVariable declarationDemosteps:

Step 1: A variable name can include spaces and special characters. In such a case, the name has to be enclosed in curly braces {}.

Assigning values to powershell variables
Assigning Values

Step 2: The properties and methods that an object can use depend on the object’s type. You can get a variable’s object type by calling its GetType method.

$myName.GetType()

Step 3: You can use the Get-Member cmdlet to see what properties and methods are available for the variable. 

$variableName | Get-Member

Step 4: Variables can store multiple objects as well. For example, you can store a list of PowerShell command in a variable, by just assigning the output of Get-Command to the variable. 

$command = Get-Command

Step 5: When you include a variable’s name inside a double-quoted string, PowerShell replaces the variable’s name with its value in the string.

"Hello, $myName"

Variable Type Casting

Highlights:Declaring a variable without specifying the data type.Arithmetic Operators in PowerShellDemosteps:

Step 1: Variables in PowerShell are weakly typed by default, they can contain any kind of data.

variable type casting
Variable Type Casting in PowerShell

Step 2: The addition of two such variables, where one contains a string and the other contains a number depends on the order in which you add the variables.

type casting examples
PowerShell Variable Type Casting Example

Step 3: This can be avoided by casting the variable with the expected data type.

casting the data types
Variable Type Casting Using Data Types

Step 4: PowerShell helps to perform mathematical functions and calculations on variabes using arithmetic operators.

  • PowerShell processes the operators in the following precedence order:
    • Parentheses(), – negative number, *, /, %, “+ or –”
  • The expressions are processed from left to right according to the precedence rules.

PowerShell Variables Data Types

  • [int] 32-bit signed integer 
  • [long] 64-bit signed integer 
  • [string] Fixed-length string of characters 
  • [char] 16-bit character 
  • [bool] True/false value 
  • [byte] 8-bit integer 
  • [double] Double-precision 64-bit floating point number 
  • [decimal] 128-bit decimal value 
  • [single] Single-precision 32-bit floating point number 
  • [array] List of similar values 

PowerShell Automatic Variables

PowerShell Automatic Variables
PowerShell Automatic Variables

Source – Microsoft

Continue Reading…

  1. PowerShell Alias
  2. PowerShell Parameters
  3. Transfer Files Between Windows & Linux Machine Using PowerShell
  4. An Introduction to VMware PowerCLI

Leave a Comment