# 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:

```powershell
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:

{% embed url="<https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-if?view=powershell-7.1>" %}

### switch

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

```powershell
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:

{% embed url="<https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-switch?view=powershell-7.1>" %}

## 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:

```powershell
$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:

```powershell
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:

```powershell
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:

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

You can find more information at the link:

{% embed url="<https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null?view=powershell-7.1>" %}

## 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:

```powershell
$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:

```powershell
@"
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:

```powershell
("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:

{% embed url="<https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.1>" %}

## 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:

{% embed url="<https://docs.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-shouldprocess?view=powershell-7.1>" %}

## 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:

```powershell
“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:

```powershell
Get-ExecutionPolicy
```

To allow script execution:

1. Run PowerShell or PowerShell ISE as an administrator.
2. Execute the command:

```powershell
Set-ExecutionPolicy Unrestricted
```

3. 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`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sherparpa.ru/en/sherpa-rpa/sherpa-designer/sherpa-designer-otvety-na-chasto-zadavaemye-voprosy/powershell/dopolnitelno.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
