Script to open remote desktop connection

I checked another thread (Automatically run a script when I log on to Windows), but it did not solve my exact problem.

I need to run a script on when a connection is made across my network using Windows Remote Desktop Connection.

The thread listed above works for the initial login, however, if I don't log out (which is necessary for some processes running on my network), then it won't run the script again the next time someone connects to the system using remote desktop connection. Previously we were using pcAnywhere to achieve this, however after running into some graphical issues with pcAnywhere, we have decided to move away from it to RDC.

Background: We need to have an email sent out anytime a connection is made to particular machines. The login name will always be the same for those systems and we do not log off when closing the connection.

Note: This tip requires PowerShell 2.0 or above.

As a PowerShell user, you probably have a PowerShell console or the ISE editor on standby. Wouldn’t it be nice to be able to just hack in a “Connect-RDP” and immediately be connected to a remote desktop when you need it? And let PowerShell deal with login credentials? Here’s how.

Securely Caching Credentials

To securely cache login credentials, you can use the command line utility cmdkey.exe. With this utility, you can save a username and a password for a given remote connection. Windows will then securely cache the information and automatically use it when needed.

Here is a function called Connect-RDP that automates the RDP connection:

function Connect-RDP { param ( [Parameter(Mandatory=$true)] $ComputerName, [System.Management.Automation.Credential()] $Credential ) # take each computername and process it individually $ComputerName | ForEach-Object { # if the user has submitted a credential, store it # safely using cmdkey.exe for the given connection if ($PSBoundParameters.ContainsKey('Credential')) { # extract username and password from credential $User = $Credential.UserName $Password = $Credential.GetNetworkCredential().Password # save information using cmdkey.exe cmdkey.exe /generic:$_ /user:$User /pass:$Password } # initiate the RDP connection # connection will automatically use cached credentials # if there are no cached credentials, you will have to log on # manually, so on first use, make sure you use -Credential to submit # logon credential mstsc.exe /v $_ /f } }

To cache credentials for a new remote desktop connection, this is how you’d call the function:

PS> Connect-RDP 10.20.30.40 -Credential testdomain\Administrator

You would then be prompted for the connection password, and the RDP connection gets initiated. Internally, Connect-RDP stores the logon information in your credential cache. So from now on, to connect to the server via RDP, you no longer need the credentials. Next time, this is all you need:

Connect-RDP 10.20.30.40

Using Multiple Connections

The function also supports multiple connections. If all of the connections require the same logon information, you can set it in one step:

PS> Connect-RDP 10.20.30.40, 10.20.30.41, 10.20.30.42 -Credential testdomain\Administrator

If the connections require different logon credentials, then set the credentials individually:

PS> Connect-RDP 10.20.30.40 -Credential testdomain\Administrator PS> Connect-RDP 10.20.30.41 -Credential testdomain\Testaccount12 PS> Connect-RDP 10.20.30.42 -Credential testdomain\Tobias

Once you have set cached credentials for all your RDP servers, you can connect to one or many with just one call:

PS> Connect-RDP 10.20.30.40, 10.20.30.41, 10.20.30.42

PowerShell will use the appropriate cached credentials for each of these connections, and opens an RDP session for each server.

Manage Cached Credentials

To manage your cached credentials, use cmdkey.exe:

PS> cmdkey Creates, displays, and deletes stored user names and passwords. The syntax of this command is: CMDKEY [{/add | /generic}:targetname {/smartcard | /user:username {/pass{:password}}} | /delete{:targetname | /ras} | /list{:targetname}] Examples: To list available credentials: cmdkey /list cmdkey /list:targetname To create domain credentials: cmdkey /add:targetname /user:username /pass:password cmdkey /add:targetname /user:username /pass cmdkey /add:targetname /user:username cmdkey /add:targetname /smartcard To create generic credentials: The /add switch may be replaced by /generic to create generic credentials To delete existing credentials: cmdkey /delete:targetname To delete RAS credentials: cmdkey /delete /ras PS> cmdkey /list:10.16.114.11 Currently stored credentials for 10.16.114.11: Target: 10.16.114.11 Type: Generic User: citrixdev\Administrator Share on:

Script to open remote desktop connection
Script to open remote desktop connection

On Windows 10, the Remote Desktop feature allows you to access a computer remotely to help other users or manage services without physically being present at the location.

While you can manage this feature through the Settings app, you can also enable Remote Desktop on Windows 10 using PowerShell. You may want to use this method to create a script to configure Remote Desktop on multiple devices quickly or send an automated script to users to set up the feature automatically without additional steps.

In this guide, you will learn the steps to use PowerShell to enable (or disable) Remote Desktop on Windows 10 and open the required firewall ports for a successful connection.

Important: Remote Desktop is only available on Windows 10 Pro. It’s not a feature available on Windows 10 Home. Also, it’s recommended that you do a full backup of your computer before proceeding, as modifying the registry can cause irreversible damage to your system.

To enable the remote desktop protocol with PowerShell, use these steps:

  1. Open Start on Windows 10.

  2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

  3. Type the following command to enable the remote desktop protocol and press Enter:

    Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 0

    Script to open remote desktop connection

  4. (Optional) Type the following command to enable remote desktop through the Windows Firewall and press Enter

    Enable-NetFirewallRule -DisplayGroup "Remote Desktop"

Once you complete the steps, you can use the Remote Desktop modern app or the old Remote Desktop Connection app to access your computer remotely, even with the firewall enabled.

Disable Remote Desktop using PowerShell

To disable the remote desktop protocol with PowerShell, use these steps:

  1. Open Start.

  2. Search for PowerShell, right-click the top result, and select the Run as administrator option.

  3. Type the following command to disable the remote desktop protocol and press Enter:

    Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server' -name "fDenyTSConnections" -value 1

    Script to open remote desktop connection

  4. (Optional) Type the following command to disable remote desktop through the Windows Firewall and press Enter:

    Disable-NetFirewallRule -DisplayGroup "Remote Desktop"

After you complete the steps, the Remote Desktop feature will be disabled, and the firewall port will be closed.

We’re focusing this guide on PowerShell, but you can use these steps to perform the same tasks using Command Prompt.