> For the complete documentation index, see [llms.txt](https://docs.devolutions.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.devolutions.net/rdm/knowledge-base/how-to-articles/execute-a-powershell-script-before-opening-a-connection.md).

# Execute a PowerShell script before opening a connection

It is possible to execute a PowerShell script before establishing a connection, to cancel the launching, and even to import external PowerShell scripts. To accomplish these actions, you would need to go into the ***Properties*** of the entry under ***Events*** – ***Before opening***, and choose ***Execute PowerShell*** in the topmost dropdown menu.

Using the $RDM.connection property, the script temporarily changes any of the session’s properties in the context of the session launch only. When the session is closed, the properties revert to their original values.

![](https://cdnweb.devolutions.net/docs/RDMW4363_2025_2.png)

Below are some examples of scripts that can be executed in this manner.

### Add date to tab title

In the ***PowerShell script*** window, copy the script provided below and click ***OK***.

```powershell
$date = Get-Date -DisplayHint Date
$RDM.Connection.TabTitle = $RDM.Connection.Name + "(" + $date + ")"
```

### Cancel session launch

To cancel the launch of a session, in the ***PowerShell script*** window of the entry, copy the script below and click ***OK*** to save.

```powershell
$RDM.Cancel = $true
```

### Run cmdlets from Devolutions PowerShell module

The Devolutions PowerShell module can be used to run cmdlets in specific entries using the `Import-Module` command.

```powershell
Import-Module DnsClient
$RDM.connection.TeamViewer.ID = (Resolve-DnsName -Name $HOST$).IPAddress
```

In this example, the script loads the DnsClient using `Import-Module`, looks up the IP address for the hostname of the entry, and writes it into the ***TeamViewer ID*** field.

### Import external PowerShell script

The `Import-Module` cmdlet can be used to call an external PowerShell script.

```powershell
Import-Module c:\Temp\RSAToken.ps1
$RSAToken = New_RSAToken
$RDM.Connection.RDP.password = $RDM.Connection.RDP.password + $RSAToken
```

In this sample, the `Import-Module` cmdlet loads the RSAToken.ps1 script and calls its function New\_RSAToken, which returns a value. Then it appends the content of the $RSAToken variable to the session’s password before opening the session.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.devolutions.net/rdm/knowledge-base/how-to-articles/execute-a-powershell-script-before-opening-a-connection.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.
