Search code examples
windowspowershellcommand-line-tool

Add Windows Credentials using PowerShell & cmdkey


I am trying to use credentials from some UI prompted to add Windows credentials using cmdkey:

$sessionCredential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "Server Crdentials")
$ps = ConvertFrom-SecureString -SecureString $sessionCredential.password
cmdkey.exe /add:server1 /user:$($sessionCredential.UserName) /pass:$($ps)

The credentials are added correctly, but the password is not.

Enter image description here

What can I do?


Solution

  • Apparently, the problem is ConvertFrom-SecureString is returning an encrypted standard string, ConvertFrom-SecureString.

    And the option to get plain text is not available on PowerShell 5.1.

    I found the correct convert here.

    I understand it is not secured. It is used inside secured clients.

    See fixed code below:

    $sessionCredential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "Server Crdentials")
    $mpass = [System.Net.NetworkCredential]::new("",$sessionCredential.password).Password
    cmdkey.exe /add:server1 /user:$($sessionCredential.UserName) /pass:$($mpass)