Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to hide the user input from read-host in Powershell?

I´m looking for a way to hide the user input from the Read-Host cmdlet.

I know I can do this with -assecurestring, but I´d like to save the input as plain text in my variable.

Is there a possible way to do this?

like image 965
Tobias Avatar asked Nov 09 '16 09:11

Tobias


People also ask

What does read-host do in PowerShell?

The Read-Host cmdlet reads a line of input from the console (stdin). You can use it to prompt a user for input. Because you can save the input as a secure string, you can use this cmdlet to prompt users for secure data, such as passwords. Read-Host has a limit of 1022 characters it can accept as input from a user.

How do you pass user input in PowerShell?

In PowerShell, the input can be retrieved from the user by prompting them with Read-Host Cmdlet. It acts as stdin and reads the input supplied by the user from the console. Since the input can also be stored as secured string, passwords can also be prompted using this cmdlet.

What is $input in PowerShell?

Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions). In the Process block of a function, the $input variable enumerates the object that is currently in the pipeline.

How do I prompt in PowerShell?

Getting the Prompt function To get the Prompt function, use the Get-Command cmdlet or use the Get-Item cmdlet in the Function drive. To get the script that sets the value of the prompt, use the dot method to get the ScriptBlock property of the Prompt function.


1 Answers

You have to use the -AsSecureString switch but you can also retrieve the plaintext value:

$securedValue = Read-Host -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securedValue)
$value = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
like image 116
Martin Brandl Avatar answered Sep 20 '22 13:09

Martin Brandl