PowerShell advanced concepts

% is an alias for foreach
? is an alias for Where-Object

$_

$_ represents the current object(s) in the pipeline.
It is very usefull because it lets us easily use the objects properties(see exaple below).

Sub operator $()

The sub operator $() as the name itself suggests is used when we have to use one expression within some other expression. For instance, embedding the output of a command with some other expression.

The sub operator basically performs te cmdlet within and treats it as a variable.

Call operator &

The (&) call operator can be used to invoke a script, command or function in PowerShell when it is written in strings.

Below is another example where a script block is put in a variable, when we call the variable itself it will be treated as plain-text. When we put the call operator & infront of it, it is performed as a script.

Special character variables ${}

Variables with specials characters such as empty space or – sign need to be enclosed in curly braces.
The curly braces direct PowerShell to interpret the variable name’s characters as literals.

If / Else

You can use the If statement to run code blocks if a specified conditional test evaluates to true. You can also specify one or more additional conditional tests to run if all the prior tests evaluate to false.

Syntax:

You can use as many “elseif” statements as you want to match multiple conditions.

Underneath is an example of a single “if” statement:

If the input does not equal “yes”, the script will stop.

A better written way would be:

Synax:

if(Boolean_expression) {
// Executes when the Boolean expression is true
}else {
// Executes when the Boolean expression is false
}
if(Boolean_expression 1) {
// Executes when the Boolean expression 1 is true
}elseif(Boolean_expression 2) {
// Executes when the Boolean expression 2 is true
}elseif(Boolean_expression 3) {
// Executes when the Boolean expression 3 is true
}

Another exmaple of a single if statement:

if ($userinput -ne "yes") 
{ 
    Write-Host "aborting script"
    Start-Sleep -Seconds 2
    exit 
}

Foreach

Nearly all programming languages have a construct called loops, with PowerShell we can loop with the foreach statement. At its most basic, a foreach loop reads an entire collection of items and foreach item, runs some kind of code.

There are technically three types of “foreach” loops:
The foreach Statement
The ForEach-Object CmdLet
The foreach() Method

The foreach Statement

Below is an example, notice the $item is an undeclared statement.
It represents the objects/items passed through the foreach loop, in other words it represents the items/objects from the $test1 array.

Another simple example:

$letterArray = "a","b","c","d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
The ForEach-Object cmdlet

ForEach-Object is a cmdlet unlike a statement, it has parameters the can be used in many different ways.

The foreach() Method

This method exists on an array or collection object/variable. It has a standard script block parameter that contains the actions.

Hashtable

A hashtable consists of a key + value.
The syntax is @{ key = value ; key = value}


Functions

The PowerShell function is used when we want to use one piece of code multiple times in a script. Whenever we execute any function, we usually type the function name.

Functions in PowerShell are categorized into two types that are written below:
– Simple Function
– Function with parameters

Syntax of a simple function:

function  <function-name>
{
code-statement-1
code-statement-2
}

Examples:

In the example below i use an empty variable “$var1”.
This variable gets used when the function is called upon.

Functions with parameters

We can create our own parameters with our functions to add extra functionality.


In the example below we create a parameter with two empty variables.
These variables can be used when executing the function.

In the example below we use a string as parameter.

Another way to execute the above function is to do: PrintName -Name Bryan

Code:

function Test-Function {
    param($a , $b)
    Write-Host $a + $b
}
Function PrintName{
param([String]$Name)
Write-Output "Hello : $name !!! "
}

Try / Catch

Leave a Reply

Your email address will not be published. Required fields are marked *