Working with variables

    Working with variables

    In PowerShell, variables are used to store and manipulate data. They can hold different types of data, such as strings, numbers, arrays, and even complex objects. Here's an overview of working with variables in PowerShell:

    Creating and assigning variables: To create a variable, simply start the variable name with a dollar sign ($) and assign a value using the equals sign (=). The variable's data type is determined automatically based on the assigned value.

    $name = "John Doe"
    $age = 30
    

    Accessing variable values: To access the value stored in a variable, use the variable name with the dollar sign ($).

    Write-Host "Name: $name"
    Write-Host "Age: $age"
    

    Modifying variable values: You can modify the value stored in a variable by reassigning it or performing operations on it.

    $age = $age + 1
    

    Variable scope: PowerShell variables have different scopes depending on where they are declared. The scopes are Global, Local, Script, and Private. By default, variables are created in the Local scope. To specify a scope, use the colon (:) after the scope name.

    $global:counter = 0
    

    Automatic variables: PowerShell has several automatic variables that are created and maintained by the system. These variables store information about the current state, preferences, or environment. Examples of automatic variables include $true, $false, $null, $_, $PSVersionTable, and $Error.

    For example, the $_ variable represents the current item in the pipeline:

    1..5 | ForEach-Object { $_ * 2 }
    

    Environment variables: Environment variables can be accessed in PowerShell using the env: scope. For example, to access the PATH environment variable, use $env:PATH.

    Write-Host "Path: $env:Path"
    

    Strongly typed variables: You can enforce a specific data type for a variable by specifying the type before the variable name. This can help prevent unexpected behavior due to data type coercion.

    [int]$integerVariable = 10
    [string]$stringVariable = "Hello, World!"
    

    Arrays and hash tables: PowerShell supports arrays and hash tables for storing multiple values.

    # Creating an array
    $array = @(1, 2, 3, 4, 5)
    
    # Accessing array elements
    Write-Host "First element: $($array[0])"
    
    # Creating a hash table
    $hashTable = @{
        Name = "John Doe"
        Age = 30
    }
    
    # Accessing hash table elements
    Write-Host "Name: $($hashTable['Name'])"
    

    These are some basic concepts for working with variables in PowerShell. As you become more familiar with PowerShell, you'll encounter more advanced features and techniques for manipulating variables and data.

    We can use variables to store the result of a command.

    For example:

     
    $Processes = Get-Process
    $Services = Get-Service
     

    To delete the value of a variable, use the Clear-Variable cmdlet or change the value to $null. To delete the variable, use Remove-Variable or Remove-Item.

    Clear-Variable -Name my_var
    Remove-Variable -Name my_var