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?
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?
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)
# then free up the unmanged memory afterwards (thank to dimizuno)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr)
–
Konikow I wasn't sure how to use -AsSecureString
as macOS with the new pwsh
(I don't know if marshalling BSTR on *nix is supported, as PtrToStringAuto()
only returned the first char of the password).
Now there's a much simpler option -MaskInput
:
$ Read-Host "Password" -MaskInput
Password: ****
abcd
© 2022 - 2025 — McMap. All rights reserved.