LoginSignup
4
4

More than 5 years have passed since last update.

Powershellでパスワード入力

Last updated at Posted at 2017-05-04

PowerShellで簡単なパスワード入力方法と入力した文字の確認する仕方を調べたので、
記載します。

PowerShellでパスワード入力と入力文字の確認

$secstr = Read-Host -Prompt "Please enter a password" -AsSecureString
if ($secstr.Length -gt 0) {
    $intptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secstr)
    $str = [System.Runtime.InteropServices.Marshal]::PtrtoStringBSTR($intptr)
    Write-Host $str
    [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($intptr)
}

重要な処理は、以下の2つです。
1. Read-Host -AsSecureStringで*付きのパスワード文字列の入力を受け付けられるようにする。
2. PowerShell上で、入力された文字を確認するために、.NETのMarshelクラスを使って変換する。(確認し終わったら、すぐにメモリを開放する)

Read-Hostの入力画面

PowerShell_ISEとPoweShellでは、Read-Hostの入力画面が異なっていたので、メモのため記載しておきます。

PowerShell_ISE
image.png

PowerShell
image.png

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4