PowerShell Variables

Variables are fundamental to any scripting language, and PowerShell is no exception. They act as containers for storing data that can be used and manipulated throughout your scripts. Understanding how to declare, assign, and use variables is a crucial first step in mastering PowerShell scripting.

What are PowerShell Variables?

In PowerShell, variables are declared by using the dollar sign ($) followed by a variable name. Variable names are case-insensitive but should follow a consistent naming convention, typically using camelCase or PascalCase.

Declaring and Assigning Variables

You can declare and assign a value to a variable in a single step. PowerShell is dynamically typed, meaning you don't need to explicitly specify the data type of the variable. PowerShell infers the type based on the assigned value.

# Assigning a string to a variable
$name = "Alice"

# Assigning a number to a variable
$age = 30

# Assigning a boolean value
$isActive = $true

# Assigning the result of a command
$processes = Get-Process

Variable Naming Conventions

While PowerShell is flexible with variable names, it's good practice to follow these guidelines:

  • Start with a letter or an underscore (_).
  • Can contain letters, numbers, and underscores.
  • Avoid using PowerShell reserved keywords.
  • Use descriptive names.

Variable Scope

Variables in PowerShell can have different scopes, which determine where in your script or session they can be accessed. Common scopes include:

  • Local: The default scope, accessible only within the current script or function.
  • Global: Accessible from anywhere in the current session.
  • Script: Accessible anywhere within the current script file.
  • Private: Accessible only within the scope in which it was created.

You can explicitly set the scope using scope modifiers:

# Declaring a global variable
$Global:myGlobalVariable = "This is global"

# Declaring a script variable
$Script:myScriptVariable = "This is for the script"

Common Variable Types

PowerShell supports a wide range of data types, including:

  • String: For text data (e.g., "Hello, World!").
  • Integer: For whole numbers (e.g., 10, -5).
  • Double/Decimal: For floating-point numbers (e.g., 3.14, 1.23e4).
  • Boolean: For true/false values ($true, $false).
  • DateTime: For date and time values.
  • Array: An ordered collection of items (e.g., @("apple", "banana", "cherry")).
  • Hashtable: A collection of key-value pairs (e.g., @{Name="John"; Age=25}).

Accessing and Manipulating Variables

You can access the value of a variable by simply using its name preceded by the dollar sign. Variables can be used in expressions, command arguments, and output.

$firstName = "Jane"
$lastName = "Doe"
$fullName = "$firstName $lastName"
Write-Host "Full name: $fullName"

$a = 10
$b = 5
$sum = $a + $b
Write-Host "The sum is: $sum"

Variable Precedence

When a variable name exists in multiple scopes, PowerShell follows a precedence order to determine which variable's value is used. The order is generally:

  1. Local
  2. Global
  3. Script
  4. AllScope
  5. Private

Understanding this precedence is important to avoid unexpected behavior in complex scripts.

Best Practices

  • Use clear and descriptive variable names.
  • Initialize variables before use, especially if their initial value is important.
  • Be mindful of variable scope to prevent unintended side effects.
  • Use type accelerators (e.g., [string], [int]) when explicit type casting is necessary for clarity or performance.