Read-ConsoleText
Asks for a line of text.
Syntax
Read-ConsoleText
[-Default <String>]
[-Description <String>]
[-Height <Int32>]
[-History <String[]>]
[-MaxLength <Int32>]
[-MaxLines <Int32>]
[-MinLength <Int32>]
[-Mode <Rich| Line| Accessible| Snapshot>]
[-Multiline]
[-ParameterName <String>]
[-Pattern <String>]
[-PatternMessage <String>]
[-Placeholder <String>]
[-PrefillDefault]
[-Required]
[[-Title] <String>]
[-Validator <ScriptBlock>]
[<CommonParameters>]#TextRead-ConsoleText
[-AllowEmpty]
[-AsPlainText]
-AsSecret
[-Description <String>]
[-Hidden]
[-MinLength <Int32>]
[-Mode <Rich| Line| Accessible| Snapshot>]
[-ParameterName <String>]
[[-Title] <String>]
[-Validator <ScriptBlock>]
[<CommonParameters>]#SecretDescription
A default is shown as a hint (Enter on an empty box accepts it, Tab puts it in the box to edit). -Required, -MinLength, -MaxLength, -Pattern and -Validator check the answer; problems show inline under the box until fixed. -History offers earlier answers with Up and Down.
Examples
Example 1
$textOptions = @{
Title = 'Server name'
Default = 'srv01'
Pattern = '[a-z][a-z0-9-]{1,14}'
PatternMessage = 'Lower case letters, digits and dashes'
}
$name = Read-ConsoleText @textOptionsA validated name
Example 2
$textOptions = @{
Title = 'Email'
Validator = { if ($_ -notmatch '@') { 'Enter an email address' } }
}
Read-ConsoleText @textOptionsA scriptblock validator ($_ is the answer; return $true, $false or an error message)
Example 3
Get-ConsoleDemo -Slug 'prompt-tour' | Select-Object -ExpandProperty SourceRead the gallery script to see Read-ConsoleText in a complete example. Each lookup returns the script's source. Use the same slug with Show-ConsoleDemo to run it.
Parameters
- -Default String
- The answer initially selected; pressing Enter accepts this value.
- -Description String
- Help text under the question.
- -Height Int32
- The visible rows of the text area (-Multiline, default 6).
- -History String[]
- Earlier answers offered with Up and Down (oldest first).
- -MaxLength Int32
- Maximum accepted answer length, measured in characters.
- -MaxLines Int32
- The most lines (-Multiline).
- -MinLength Int32
- Minimum accepted answer length, measured in characters.
- -Mode InteractionMode
- Forces how to ask: Rich (in-place prompt), Line (question and typed answer), Accessible (numbered, no redraws) or None (use the default or fail).
- Possible values:
Rich,Line,Accessible,Snapshot - -Multiline SwitchParameter
- Asks for several lines of text in a text area: soft wrap, Enter for a new line, Ctrl+Enter (or Ctrl+S) to save, paste keeps line breaks. In line mode the answer ends with an empty line.
- -ParameterName String
- The parameter of your script that supplies this value without a prompt (named in the non-interactive error, for example -Domain).
- -Pattern String
- A regular expression the whole answer must match.
- -PatternMessage String
- The message when -Pattern does not match.
- -Placeholder String
- Muted text in the empty box.
- -PrefillDefault SwitchParameter
- Puts the default in the box (editable) instead of showing it as a hint.
- -Required SwitchParameter
- Refuses an empty answer.
- -Title String
- The prompt displayed above the input control.
- -Validator ScriptBlock
- A validator: $_ is the answer; return $true (or nothing) to accept, $false or an error message to refuse.
- -AllowEmpty SwitchParameter
- With AsSecret, accepts an empty answer.
- -AsPlainText SwitchParameter
- With AsSecret, explicitly returns plaintext.
- -AsSecret SwitchParameter
- Reads a masked secret and returns SecureString by default. History and defaults are forbidden.
- With AsSecret, hides even the typed length.
Common Parameters
This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
For more information, see about_CommonParameters.