Read-Host
從主控台讀取輸入的一行。
Syntax
Read-Host
[[-Prompt] <Object>]
[-AsSecureString]
[<CommonParameters>]
Description
Cmdlet Read-Host
會從控制台讀取一行輸入。 您可以使用它來提示使用者輸入。 由於您可以將輸入儲存成安全字串,因此您可以使用這個 Cmdlet 來提示使用者輸入安全資料 (例如密碼) 和共用的資料。
範例
範例 1:將主控台輸入儲存至變數
此範例會顯示字串「請輸入您的年齡:」作為提示。 輸入值並按下 Enter 鍵時,值會儲存在變數中 $Age
。
$Age = Read-Host "Please enter your age"
範例 2:將主控台輸入儲存為安全字串
此範例會顯示字串 “Enter a Password:” 作為提示。 當輸入值時,星號 (*
) 會出現在控制臺上取代輸入。 按下 Enter 鍵時,值會儲存為變數中的 $pwd_secure_string
SecureString 物件。
$pwd_secure_string = Read-Host "Enter a Password" -AsSecureString
參數
-AsSecureString
表示 Cmdlet 會顯示星號 (*
) 取代使用者輸入為輸入的字元。 當您使用此參數時,Cmdlet 的 Read-Host
輸出是 SecureString 物件, (System.Security.SecureString) 。
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Prompt
指定提示的文字。
輸入字串。
如果字串包含空格,請將它括在引號中。
PowerShell 會將冒號 (:
) 附加至您輸入的文字。
Type: | Object |
Position: | 0 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
輸入
None
您無法使用管線傳送輸入至此 Cmdlet。
輸出
System.String or System.Security.SecureString
如果使用 AsSecureString 參數, Read-Host
則會傳回 SecureString。 否則,它會傳回字串。