Skip to content
ConsoleMonster
Cmdlet

Read-ConsoleText

Module
ConsoleMonster
Outputs
System.String System.Security.SecureString

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>]
#
Parameter set: Text
Read-ConsoleText
[-AllowEmpty]
[-AsPlainText]
-AsSecret
[-Description <String>]
[-Hidden]
[-MinLength <Int32>]
[-Mode <Rich|Line|Accessible|Snapshot>]
[-ParameterName <String>]
[[-Title] <String>]
[-Validator <ScriptBlock>]
[<CommonParameters>]
#
Parameter set: Secret

Description

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 @textOptions

A validated name

Example 2

$textOptions = @{
    Title = 'Email'
    Validator = { if ($_ -notmatch '@') { 'Enter an email address' } }
}
Read-ConsoleText @textOptions

A scriptblock validator ($_ is the answer; return $true, $false or an error message)

Example 3

Get-ConsoleDemo -Slug 'prompt-tour' | Select-Object -ExpandProperty Source

Read 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 optionalsets: Text
The answer initially selected; pressing Enter accepts this value.
-Description String optional
Help text under the question.
-Height Int32 optionalsets: Text
The visible rows of the text area (-Multiline, default 6).
-History String[] optionalsets: Text
Earlier answers offered with Up and Down (oldest first).
-MaxLength Int32 optionalsets: Text
Maximum accepted answer length, measured in characters.
-MaxLines Int32 optionalsets: Text
The most lines (-Multiline).
-MinLength Int32 optional
Minimum accepted answer length, measured in characters.
-Mode InteractionMode optional
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 optionalsets: Text
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 optional
The parameter of your script that supplies this value without a prompt (named in the non-interactive error, for example -Domain).
-Pattern String optionalsets: Text
A regular expression the whole answer must match.
-PatternMessage String optionalsets: Text
The message when -Pattern does not match.
-Placeholder String optionalsets: Text
Muted text in the empty box.
-PrefillDefault SwitchParameter optionalsets: Text
Puts the default in the box (editable) instead of showing it as a hint.
-Required SwitchParameter optionalsets: Text
Refuses an empty answer.
-Title String optionalposition: 0
The prompt displayed above the input control.
-Validator ScriptBlock optional
A validator: $_ is the answer; return $true (or nothing) to accept, $false or an error message to refuse.
-AllowEmpty SwitchParameter optionalsets: Secret
With AsSecret, accepts an empty answer.
-AsPlainText SwitchParameter optionalsets: Secret
With AsSecret, explicitly returns plaintext.
-AsSecret SwitchParameter requiredsets: Secret
Reads a masked secret and returns SecureString by default. History and defaults are forbidden.
-Hidden SwitchParameter optionalsets: Secret
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.