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.

Below are some examples of scripts that can be executed in this manner.
In the PowerShell script window, copy the script provided below and click OK.
$date = Get-Date -DisplayHint Date
$RDM.Connection.TabTitle = $RDM.Connection.Name + "(" + $date + ")"
To cancel the launch of a session, in the PowerShell script window of the entry, copy the script below and click OK to save.
$RDM.Cancel = $true
The Import-Module
cmdlet can be used to call an external PowerShell script.
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.