PowerShell: Remote Desktop AutoLogon Script

14 mar 2024 2 min di lettura
PowerShell: Remote Desktop AutoLogon Script
Indice dei contenuti


In the fast-paced world of IT and remote work, efficiency and time-saving techniques are key. For professionals who frequently connect to remote desktops, the process of manually entering login credentials can be a tedious and time-consuming task. To address this challenge, a PowerShell script offers an automated solution for accessing Remote Desktop connections, saving time and improving productivity.

The PowerShell script

The provided PowerShell script automates the process of logging into a Remote Desktop Connection (RDC). It cleverly uses a series of cmdlet commands to streamline the authentication process, allowing users to connect to their remote desktop without the need to manually enter their credentials every time.

## PowerShell Script to auto login to remote desktop

 # Set variables for server address, username, and password
 # Keep password in single quote to handle special character
 $Server = "rdc.example.com"
 $User = "username"
 $Password = 'password'

 # Remove any existing Remote Desktop Connection credentials from Windows Credential Manager
 cmdkey /list | ForEach-Object {
 if ($_ -like "*target=TERMSRV/*") {
 $credentialTarget = $_ -replace " ", "" -replace "Target:", ""
 cmdkey /del:$credentialTarget

 # Announce the initiation of a connection to the specified Remote Desktop
 echo "Connecting to $Server"

 # Save the Remote Desktop connection credentials using cmdkey
 cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password

 # Initiate a Remote Desktop connection to the specified server
 mstsc /v:$Server

How the screenplay works

Setting connection parameters: The first step is to configure the remote desktop system host address and login credentials. You can set variables for the server address, username, and password.


Deleting existing credentials - So the code starts with the list of all saved credentials using cmdkey /list and then filters the credentials for the remote desktop connection using the "target=TERMSRV/" pattern. Then delete these credentials to ensure that the new login information is used.

cmdkey /list | ForEach-Object{
 if($_ -like "*target=TERMSRV/*"){
 cmdkey /del:($_ -replace " ","" -replace "Target:","")

Show a message - The script prints a message on the screen indicating that the connection to the remote desktop has been initiated.

# Announce the initiation of a connection to the specified Remote Desktop
 echo "Connecting to $Server"

Saving credentials - The script uses cmdkey to create a generic credential entry for the remote desktop server. This step saves your username and password, effectively automating the login process.

cmdkey /generic:TERMSRV/$Server /user:$User /pass:$Password

Initiating the remote desktop connection: Finally, the script calls mstsc with the /v: parameter to specify the server address, initiating the remote desktop connection.

mstsc /v:$Server

Implementation phases

To implement this script:

  1. Save the above script to a file like connect_remote_desktop.ps1
  2. Replace "rdc.example.com", "username", and "password" with the actual remote desktop server address, username, and password.
  3. Open PowerShell with administrator privileges.
  4. Go to the script directory.
  5. Type .\connect_remote_desktop.ps1 to connect the remote desktop.

Alternatively, simply save the script file and the correct script to the script, then select "Run with PowerShell".


This script greatly simplifies the process of connecting to a remote desktop, especially for IT professionals and remote workers who frequently switch between servers. However, users should ensure their credentials are stored securely and consider the security implications of automating login processes, especially in environments with strict IT security policies.

Ultimately, by leveraging PowerShell, professionals can enjoy a more efficient and streamlined workflow by focusing on core tasks rather than the repetitive process of manual logins. This script is a testament to the power of automation in the modern workplace.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.