Prompt for password without using Get-Credential or Read-Host -AsSecureString, but not display text entered on screen RRS feed

  • Question

  • I'm automating the creation of some scheduled tasks by calling schtasks. I need to supply a password for the /rp switch, but schtasks won't take System.Security.SecureString text. So Get-Credential is out, as is Read-Host -AsSecureString. I can use Read-Host, but I'm really looking for a way to obfuscate the display of the text entered.


    Friday, September 2, 2011 3:33 PM


  • You can convert a securestring to plain text:

    $password = Read-Host -assecurestring "Please enter your password"
    $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))

    Friday, September 2, 2011 3:36 PM

All replies

  • I should add that if there is a better to create the scheduled task other than using schtasks, I'm open to that as well.
    Friday, September 2, 2011 3:34 PM
  • You can convert a securestring to plain text:

    $password = Read-Host -assecurestring "Please enter your password"
    $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))

    Friday, September 2, 2011 3:36 PM
  • Wow - awesome!

    Thanks for the quick response and info!

    Friday, September 2, 2011 3:41 PM
  • As variant with Ger-Credential:

    $password = (Get-Credential).GetNetworkCredential().password

    • Proposed as answer by Rich Prescott Friday, September 2, 2011 3:46 PM
    Friday, September 2, 2011 3:42 PM
  • In addition, I personally use schtasks.exe to manage scheduled tasks in PowerShell, but after you asked your question, I wondered if there might be a better way. It looks like there is a COM object available for such a thing, IF your machines are Vista or newer:


    It's not an option for me since a most of my machines are still XP, but something to consider for the future, eh? :)


    Friday, September 2, 2011 3:46 PM
  • Ah - that's cool. I missed that in my searching. The one big thing I have left is that you apparently can't set the description using schtasks, but that's minor.

    I'm actually creating tasks on 2008 R2 servers, so I'll certain noodle with the task scheduler API a little. Maybe do a function.


    Friday, September 2, 2011 3:50 PM
  • You may download PowerShellPack - http://archive.msdn.microsoft.com/PowerShellPack.  PowerShell pack contains TaskScheduler module  (List scheduled tasks, create or delete tasks).
    Friday, September 2, 2011 3:55 PM
  • #$domainUser is your machine\username
    #$password is your password
    #$credential is the credential System.Management.Automation.PSCredential object to be used in other context
    $passwordSecure = $password | ConvertTo-SecureString -asPlainText -Force;
    $credential = New-Object System.Management.Automation.PSCredential($domainUser, $passwordSecure);

    Tuesday, August 13, 2019 7:54 PM