Additional#

Logical Constructs#

if, elseif, else#

In PowerShell, conditional operators allow the program to make decisions and execute different blocks of code based on conditions.

  • The if() {} construct checks the condition in parentheses and executes the block of code if the condition is true.
  • The keywords elseif{} and else{} are used to add additional checks and alternative actions.
  • The elseif and else blocks are optional: you can use just if.

Example:

if ($number -gt 10) {
    Write-Output "The number is greater than 10"
} elseif ($number -eq 10) {
    Write-Output "The number is equal to 10"
} else {
    Write-Output "The number is less than 10"
}

If the condition in if is true, subsequent elseif and else are skipped.

You can use multiple elseif in a row to check several options.

The else block executes when all previous conditions are false.

You can find more information at the link:

everything about ifdocs.microsoft.com

switch#

The switch statement is a convenient construct for checking a variable against multiple values and selecting the corresponding block of code.

switch ($variable) {
    0 { Write-Output "The value is 0" }
    1 { Write-Output "The value is 1" }
    2 { Write-Output "The value is 2" }
    default { Write-Output "The value does not match any conditions" }
}
  • For each value, a block of code is listed that will be executed if the variable's value matches.
  • The default block is optional and executes if none of the values matched.
  • switch is convenient for multiple choice options, reducing nested if constructs.

You can find more information at the link:

everything about switchdocs.microsoft.com

Checking for $null#

In PowerShell, it is recommended to always place $null on the left side of the expression when checking for equality with $null:

$null -eq $value

This is due to the way values are handled in PowerShell, to avoid unexpected errors if $value is not defined.

In logical expressions, PowerShell can use cmdlets and functions that will be evaluated as boolean values based on their truthiness/falsiness. For example, the condition:

if ($null_object) {
    $False
} else {
    $True
}

Will consider $null_object as a false condition (if empty or $null) and execute the corresponding block.

It is also possible to assign values to variables directly within the condition, which is convenient for simultaneous assignment and checking for $null. For example:

if ($var = (function)) {
    $True
} else {
    $False
}

Here, the variable $var is assigned the result of the function, and it is immediately checked whether it is $null (or empty).

Similarly, checking a variable against itself:

if ($var = $var) {
    $True
} else {
    $False
}

You can find more information at the link:

everything about nulldocs.microsoft.com

Substitution Strings#

In PowerShell, there are several ways to form and concatenate strings.

Quotes#

Variables can be inserted directly into strings with different types of quotes:

$string = "’object’"
$string1 = "sold"

"Test’ $string successfully”” $string1”   # Result: Test’ ‘object’ successfully” sold

'Test’ '+$string+' successfully’’ '+$string1  # Result: Test’ ‘object’ successfully’ sold

HereString#

You can use HereString to declare a multiline string while preserving formatting:

@"
Test+@’ $string successfully” $string1
"@

The result will strictly correspond to what is contained inside.

F-strings#

There are also F-strings for formatting strings with parameters:

("Test {0} successfully {1}" -f $string, $string1)

PSCustomObject#

PSCustomObject is a PowerShell object that stores data in a hash table-like manner, but can be manipulated as an object by accessing fields via dot notation.

This allows for convenient data structuring, creating objects with desired properties, and performing operations.

You can find more information about PSCustomObject at the link:

everything about pscustomobjectdocs.microsoft.com

Credentials#

In PowerShell, there are special mechanisms for securely working with credentials, including built-in functions and parameters, as well as constructs that inform the user of changes and allow for process management (e.g., ShouldProcess).

You can find more information about ShouldProcess at the link:

everything about shouldprocessdocs.microsoft.com

Error Running .ps1 Files#

The error when running .ps1 files in PowerShell is related to the execution policy, which by default prevents the execution of any scripts to protect the system from malicious code.

The error message looks like this:

“Cannot load file ... .ps1 because script execution is disabled on this system.”

The reason is that Windows uses Execution Policy — a set of rules that determines which scripts can be run. By default, the policy is set to Restricted, which blocks scripts.

You can check the current policy with the command:

Get-ExecutionPolicy

To allow script execution:

  1. Run PowerShell or PowerShell ISE as an administrator.
  2. Execute the command:
Set-ExecutionPolicy Unrestricted
  1. Confirm the change by pressing "Yes".

This will allow running .ps1 scripts without restrictions.

For stricter control, you can choose other policy levels, such as RemoteSigned or AllSigned.