# PowerShell Training

PowerShell is a powerful scripting language and a versatile command shell widely used for automating and simplifying tasks in the Windows environment. Expressions in the properties of Sherpa Designer blocks are written in a modified version of PowerShell, and the blocks themselves (their internal code) are written in PowerShell. For No-Code use of Sherpa Designer, knowledge of PowerShell is not required. However, for a Low-Code approach, it is advisable to acquire some basic knowledge of this language, as it will allow you to create more concise diagrams and solve non-standard tasks.

***

## 1. PowerShell Basics and Comparison with C# and VB

PowerShell has a rich set of tools for working with objects and their methods. It allows you to work with all classes and methods of the Microsoft .NET Framework. The difference between using PowerShell and other .NET programming languages, such as C# and VB, lies in the syntax.

For example, in C#, the string to get information about the language and regional settings can be executed with the command:

```
var cultureInfo = System.Globalization.CultureInfo.GetCultureInfo("ru-RU")
```

In PowerShell, such a command would look like this:

```
$cultureInfo = [System.Globalization.CultureInfo]::GetCultureInfo("ru-RU")
```

In Visual Basic .NET:

```
Dim cultureInfo As System.Globalization.CultureInfo = System.Globalization.CultureInfo.GetCultureInfo("ru-RU")
```

or

```
Dim cultureInfo As New System.Globalization.CultureInfo("ru-RU")
```

## 2. Comparison Operators in PowerShell

Effective interaction with data in PowerShell is based on the use of comparison operators. These tools are integrated into Sherpa RPA, expanding automation capabilities.

Working with comparison operators and patterns:

* `*-c..` — case-sensitive;
* `-eq` — checks if two values are equal;
* `-ne` — checks if two values are not equal;
* `-gt` — checks if the first number is greater than the second;
* `-ge` — checks if the first number is greater than or equal to the second;
* `-lt` — checks if the first number is less than the second;
* `-le` — checks if the first number is less than or equal to the second;
* `-like` — checks if the text contains a specific word or pattern. You can use `?` and `*` for pattern matching (e.g., `*.txt` — all files with the `.txt` extension);
* `-notlike "#find text#"` — checks that the text does NOT contain a specific word or pattern;
* `-match "#pattern#"` — uses regular expressions to search for complex patterns in text;
* `-is [type]` — checks what type the object is (e.g., string, number);
* `-isnot [type]` — checks that the object is NOT of that type.

These operators help create more understandable and convenient scripts for data analysis and processing.

## 3. Working with Collections and Basic Operations

PowerShell is well-suited for working with various data structures such as arrays, lists, dictionaries, data tables, and strings. For example, to filter attachments in a message:

<pre class="language-powershell"><code class="lang-powershell"><strong>($Mail.Attachments | Where-Object {$_.DisplayName -Contains "text"}).Length > 0
</strong></code></pre>

This example shows how to filter attachments by name and check if such attachments exist.

## 4. Declaring Variables and Type Casting

Examples of expressions and commands for working with variables and type casting:

* type conversion to the first argument:
  * `'1' + 1` gives `'11'` — string concatenation, since the first argument is a string, the number is converted to a string, and then concatenation occurs;
  * `1 + '1'` gives `2` — numeric addition, because in this case the second argument is converted to a numeric type, and both addends become numbers;
* `"5/7/07" -as [DateTime]` and `[datetime]"5/7/07"` — ways to convert a date string (e.g., "5/7/07") to a special data type "Date and Time";
* `[Convert]::ToDateTime("5/7/07")` — another way to convert a date string (e.g., "5/7/07") to a special data type "Date and Time";
* `(1+12).ToString()` — an expression that performs a mathematical operation, and then the result changes its data type to a string: `'13'`;
* `Ctrl+Space` — a command that automatically completes code or shows helpful hints for continuing to write the script;
* `$null` — "null value", used to indicate that a variable or result is absent;
* `[string]::IsNullOrEmpty()` — a command that checks if a string is empty or equal to null. If so, it returns `true`, otherwise — `false`.

PowerShell supports working with any data types, similar to C#:

* create a variable `$MyListInList` with the type `Object` (to store any data type);
* use the command below to create a list:

`$script:MyListInList = New-Object Collections.Generic.List[Collections.Generic.List[string]];`

* add two empty lists to the list.

First list:

`$MyListInList.Add((New-Object Collections.Generic.List[string]));`

Second list:

`$MyListInList.Add((New-Object Collections.Generic.List[string]));`

* add data to the nested lists, for example, the string `"myval1"` to the first list and `"myval2"` to the second:

`$MyListInList[0].Add("myval1"); $MyListInList[1].Add("myval2");`<br>

Note that the option "Local and Global Variables" may be enabled in the system:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcooC2c0QDvaI4YaM67tASAUl-zq2I1jRHfgTvkhGx2uLwBR98dtFQFZXzgB9W8_4TnNEOhAZTdohYVTlQYF0pi4hX0SdCW9wimuGQwqX3lQg4ANX2lr2GoKdpBeS3KQ8CvLPHyLg?key=lj_Rcgf7GmeThGb8eC0j_g" alt=""><figcaption></figcaption></figure>

If this option is enabled, variables created inside a block are by default considered "local" and are visible only within that block. If the option "All Variables are Global" is enabled, then variables can be accessed and used anywhere in the program or script, regardless of the code block or function in which they were created.

## 5. Variable Scope Inside the "Execute Expression" Block

When you write commands inside a block (for example, in the "Execute Expression" block) and the option "Local and Global Variables" is enabled, the variables created there are by default considered "local" (visible only within that block).

For example, the variable `$test2` created inside such a block will not be accessible outside of it:

<figure><img src="https://3237142148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FscP4BXwl9ufpJr5mfVln%2Fuploads%2Fgit-blob-a7d45d9534367b8f12b74747325cc44960217a55%2Fimage%20(73).png?alt=media" alt=""><figcaption></figcaption></figure>

If you want the variable `$test2` to be accessible even after executing this block (for example, from another part of your script), you need to explicitly specify that it belongs to the "script" scope (the entire script, not just the block). To do this, use the notation:

`if ($test -eq 100) { $script:test2 = 100 }`

Note the `$script:`; this expression indicates that the variable `$test2` is created in the scope of the entire script, and it can be accessed outside the block.

<figure><img src="https://3237142148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FscP4BXwl9ufpJr5mfVln%2Fuploads%2Fgit-blob-237d0d1f170010583f60ca4976d6dcf03ee57893%2Fimage%20(74).png?alt=media" alt=""><figcaption></figcaption></figure>

> When you read the value of a variable, PowerShell first looks for it inside the block (locally). If it is not there, it looks in the script scope. But if you want to assign a value to a variable and make it available outside the block, be sure to specify the scope `$script:`

## 6. Using Local and Remote PowerShell Console in Sherpa RPA Designer for Expression Testing and Script Debugging

When working with scripts in Sherpa RPA, you may sometimes need to test commands or debug their operation. For this, you can use the console.

In the lower central panel of Sherpa Designer, you can open the console by clicking on the "Console" tab. This will allow you to enter commands and see the results of their execution. You can clear the console using the "Clear" button or reload the console using the "Reload Console" button to reset the current state.

Clearing and reloading: You can clear the console using the "Clear" button or reload the console using the "Reload Console" button to reset the current state.\
\
The local console is a built-in console that runs directly in the Sherpa RPA window. It allows you to quickly test individual commands or expressions. It is well-suited for testing and debugging small sections of code.

When the Robot is not yet running, only the local console is available. The local console allows you to check commands, expressions, and conditions:<br>

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXftLMOhzlZDMVl1ol-J_IcXwf_XCR2b7rVbZxkvIVBa7oxRrrydUqxBQ5ZliSZjl8RhIsffG5wpSi5qYCsN1eBnBMX0aISdExR7wzF5w5PWEw1G9SFdaCoPAiho5Ev0aZCuTDaIaQ?key=lj_Rcgf7GmeThGb8eC0j_g" alt=""><figcaption></figcaption></figure>

If you run the Robot in debug mode and pause it, you can work with the remote console (which is the context of the Robot itself, all commands are executed in its PowerShell stack), allowing you to view the Robot's current variables, modify them, and check expressions considering the input data. To pause the Robot, the User needs to:

* set breakpoints. To do this, they should right-click on the desired block and:

  * select  from the context menu;
  * click on the desired block and press F9 on the keyboard;
  * click the "Breakpoint" button in the "Debug" menu: ![](https://3237142148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FscP4BXwl9ufpJr5mfVln%2Fuploads%2Fgit-blob-9fcb12ff20729c5f87ec30abfe90d41a82c701e0%2F2025-07-21_20-55-06.png?alt=media).

  The block will be highlighted with a red frame:
* click the button . The Robot will stop at the block of the diagram where the breakpoint is set;
* enter the variable name (or another command for working with the remote console):

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcbADAsss3iRaF1tahAWJxSJTrsrsd8tCmXEW5LjlUgOXzW4H1TZb2bHCpFfFi3r_Q8NIJknqh5n9eyluxS_HZQdJPFwVIfXWThZpp_robxg6HZg7d7maCXY8egfqII3y6z3AO9LQ?key=lj_Rcgf7GmeThGb8eC0j_g" alt=""><figcaption></figcaption></figure>

Accordingly, control in the console occurs through two streams: local (“L”) and remote (“R”). When the Robot is started and paused, the remote console (in the Robot) is automatically activated. It is also possible to switch between these streams; to do this, you need to click the corresponding button: “L” or “R” while the Robot is paused:

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfv7hM7JrFUW9Nid0Wr1ft6p1ZaF7pUPytjZNv8FvYDoI8sPMQ_fzGEyb-CzNTsVUjcYfvmIu5FXLDXA0q-5mB4ZiFVTifq_YbViP5ikN-RwEy_T0VvdXcu22_28PxQay0UG9A9?key=lj_Rcgf7GmeThGb8eC0j_g" alt=""><figcaption></figcaption></figure>

Examples of usage:

* Checking variable values: Enter the variable name, for example, `$myVariable`, to see its current value.
* Methods and properties available for this variable: Enter the variable name, for example, `$myVariable`, then enter a dot `.` and press Ctrl+Space.
* Executing expressions: You can execute more complex expressions, such as `$myArray | Where-Object { $_ -eq "value" }`, to filter arrays.
* Debugging functions: If you have functions, you can call them from the console to check their operation, for example, `MyFunction -Parameter $myParameter`.

**Working in the console is demonstrated in the following video:**

{% embed url="<https://sherparpa.ru/ucontent/?02>" %}

**How to use IntelliSense hints in the Sherpa Designer console is demonstrated in the following video:**

{% embed url="<https://sherparpa.ru/ucontent/?A9zG>" %}

## 7. Creating Custom Blocks in the Sherpa RPA Block Editor

If you want to use your own functions in Sherpa RPA, you have the option to create your own blocks.

An example of how this is done using PowerShell and an external library (.dll file):

* Connecting an external library:

`Add-Type -Path "e:\123\1\TestDialog.dll"`

* Creating an object of a class from the library:

`$mycls = [TestDialog.Class1]::new();`

* Calling dialog methods:

`$mycls.MyDialog1($MyText);`

* Running the static method `MyDialog2`, which takes the variable `$MyText`:

`[TestDialog.Class1]::MyDialog2($MyText);`

* Working with data tables:

`$dt = $dt.DefaultView.ToTable($false, "Column1", "Column2")`

A data table is formed with columns "Column1" and "Column2".


---

# 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/obuchenie-po-razrabotke-na-platforme-sherpa-rpa/obuchenie-powershell.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.
