2

I need to be able to call the exe and specify the servername and username only. I still want it to prompt for the password like normal. When I call the first command below the RDP will prompt me for my password, but it already has my local username inserted. If I wanted to use some one else's username, I can't get it to work correctly in the second command.

Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList "/v:$server"

Start-Process "$env:windir\system32\mstsc.exe" -ArgumentList "/v:$server/IP:$username"

How do I pass in just the servername and the username?

CuriousOne
  • 922
  • 1
  • 10
  • 26

1 Answers1

1

mstsc.exe does not have a /username parameter. It does, however, have a /prompt argument which will prompt for username and password. Otherwise, you need to use a .rdp connection file.


Update:
You can utilize cmdkey to preload credentials so mstsc does not prompt you when you remote into a host, and then prompt for credentials using built-in functionality such as Read-Host or Get-Credential yourself.

Example function:

function Connect-Host
{
    param
    (
        [Parameter(Position = 0, Mandatory)]
        [ValidateScript({Test-Connection -ComputerName $PSItem -Quiet})]
        [string]
        $ComputerName,

        [Parameter(Position = 1)]
        [System.Management.Automation.CredentialAttribute()]
        [pscredential]
        $Credential = (Get-Credential)
    )

    $cmdKeyArgs = @(
        "/generic:TERMSRV/$ComputerName"
        "/user:$($Credential.UserName)"
        "/pass:$($Credential.GetNetworkCredential().Password)"
    )
    $null = & "$Env:SystemRoot\System32\CMDKEY.exe" @cmdKeyArgs

    & "$Env:SystemRoot\System32\MSTSC.exe" /v:$ComputerName
}

This can be modified so it doesn't prompt you every time you run it since it will cache your credentials with cmdkey. Additional logic can be used to query cmdkey for the computer you're connecting to prior to prompting/saving.

Example of this:

cmdkey /list |
    Select-String -Pattern $ComputerName -Context 2 |
    Select-Object -Property @(
         @{N='ComputerName';E={$ComputerName}}
         @{N='User';E={$PSItem.Context.PostContext[-1] -replace '\s*User:\s*'}}
    )
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • So there's no way I can specify a username for the RDP to use without having an .rdp file? – CuriousOne Aug 10 '17 at 18:44
  • @CuriousOne Correct. – Maximilian Burszley Aug 10 '17 at 18:53
  • I just tried this on my Win10 laptop. Doesn't really work. Or at least not the way I wanted it to. I wanted it to prompt for BOTH user name AND password. But adding `/prompt` to the mstsc.exe call doesn't actually force a username prompt. It still has the user name prefilled with what I used the previous time to connect. And I manually have to first click the `More choices` link and then the `Use a different account`link to get to the user name prompt. – StackzOfZtuff Aug 01 '18 at 12:13
  • @StackzOfZtuff It's been a while, but I'm going to update this answer with a solution I've come up with in the past year. See my edit now – Maximilian Burszley Aug 01 '18 at 12:51
  • 1
    @CuriousOne If you're still active, I've updated this answer to meet your use-case better. – Maximilian Burszley Aug 01 '18 at 12:54