Transfer credentials to mstsc.exe via parameters
2 posts
Hello,
I have two accounts. I usually work with a user account. When I want to initiate an RDP connection with an admin account from the Network Scanner, I have to enter the credentials for my admin account every time. It is inconvenient.
Is there a way to transfer the credentials via the parameters to
mstsc.exe /admin /v:[host name]
Unfortunately, VB scripts are not permitted in our environment. Batch and PowerShell scripts are allowed.
I would be grateful for any help.
Unfortunately, mstsc.exe doesn't support passing credentials directly via command-line parameters for security reasons. However, there are several practical workarounds using batch or PowerShell scripts:
Solution 1: Using cmdkey (Simplest)
You can use the cmdkey command to store credentials in Windows Credential Manager, then launch RDP.
Batch script:
@echo off set HOST=%1 set USERNAME=DOMAIN\AdminUser cmdkey /generic:TERMSRV/%HOST% /user:%USERNAME% /pass:YourPassword mstsc /admin /v:%HOST% cmdkey /delete:TERMSRV/%HOST%
PowerShell script:
param(
[string]$HostName
)
$username = "DOMAIN\AdminUser"
$password = "YourPassword"
# Add credentials to Credential Manager
cmdkey /generic:"TERMSRV/$HostName" /user:$username /pass:$password
# Launch RDP
mstsc /admin /v:$HostName
# Optional: Remove credentials after connection
Start-Sleep -Seconds 2
cmdkey /delete:"TERMSRV/$HostName"
Solution 2: Storing Password Securely (Recommended)
Instead of hardcoding the password, store it encrypted.
PowerShell with encrypted password:
param(
[string]$HostName
)
# One-time setup: Create encrypted password file
# Read-Host "Enter admin password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\Path\AdminPass.txt"
$username = "DOMAIN\AdminUser"
$encryptedPassword = Get-Content "C:\Path\AdminPass.txt"
$securePassword = $encryptedPassword | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PSCredential($username, $securePassword)
$plainPassword = $credentials.GetNetworkCredential().Password
cmdkey /generic:"TERMSRV/$HostName" /user:$username /pass:$plainPassword
mstsc /admin /v:$HostName
Start-Sleep -Seconds 2
cmdkey /delete:"TERMSRV/$HostName"
Solution 3: Create .RDP Files
Generate an .rdp file with saved credentials:
param(
[string]$HostName
)
$rdpContent = @"
full address:s:$HostName
username:s:DOMAIN\AdminUser
administrative session:i:1
prompt for credentials:i:0
"@
$rdpFile = "$env:TEMP\temp_$HostName.rdp"
$rdpContent | Out-File -FilePath $rdpFile -Encoding ASCII
# Store credentials
cmdkey /generic:"TERMSRV/$HostName" /user:"DOMAIN\AdminUser" /pass:"YourPassword"
# Launch RDP
Start-Process $rdpFile
# Cleanup
Start-Sleep -Seconds 2
cmdkey /delete:"TERMSRV/$HostName"
Remove-Item $rdpFile
Integration with Network Scanner
For Batch file
Save the batch script as C:\path\to\rdp-admin.bat and configure Network Scanner with:
cmd.exe /k C:\path\to\rdp-admin.bat %0
The batch file will receive the IP address as %1 parameter.
For PowerShell script
Save the PowerShell script as C:\path\to\RDPConnect.ps1 and configure Network Scanner with:
powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\RDPConnect.ps1" -HostName %0
Or if you prefer to call PowerShell from a batch wrapper:
cmd.exe /k powershell.exe -ExecutionPolicy Bypass -File "C:\path\to\RDPConnect.ps1" -HostName %0
Security notes:
- The encrypted password method (Solution 2) only works for the user who created it on that specific machine.
- Consider using Windows Credential Manager permanently instead of storing passwords in scripts.
- The brief window where credentials exist in Credential Manager is a minor security consideration.