New-ConsoleField
Creates a form field for New-ConsoleForm.
Syntax
New-ConsoleField
[-ArgumentList <Object[]>]
[-AsSecret]
[-Badge <Object>]
[-Choices <Object[]>]
[-CommandPlaceholder <String>]
[-CommandValue <Object>]
[-Default <Object>]
[-DependsOn <String[]>]
[-Describe <Object>]
[-DisabledReason <Object>]
[-Display <Object>]
[-Extension <String[]>]
[-GroupBy <Object>]
[-Height <Int32>]
[-Help <String>]
[-Integer]
[-Key] <String>
[-Kind <Text| Number| Toggle| Select| Path>]
[[-Label] <String>]
[-LoadChoices <ScriptBlock>]
[-Maximum <Decimal>]
[-MaxLength <Int32>]
[-MaxLines <Int32>]
[-Minimum <Decimal>]
[-MinLength <Int32>]
[-Multiline]
[-Multiple]
[-MustExist]
[-NoRemember]
[-OffText <String>]
[-OnText <String>]
[-Page <String>]
[-ParameterName <String>]
[-PathKind <Any| File| Folder>]
[-Pattern <String>]
[-PatternMessage <String>]
[-Placeholder <String>]
[-Preview <ScriptBlock>]
[-Required]
[-Step <Decimal>]
[-Validator <ScriptBlock>]
[-VisibleWhen <ScriptBlock>]
[<CommonParameters>]#All Parameter SetsDescription
A field has a name (its key in the result), a label, help shown while it has focus, a default, validation (-Required, -Validator and the type's own checks) and the command-line parameter it supplies (-ParameterName) for the equivalent command. -VisibleWhen shows it only while a condition on the other values holds ($Values). -DependsOn resets it when those fields change; with -LoadChoices its options are loaded again in their own runspace (a spinner shows, Esc cancels), from the current values ($Values). Choices are built from any objects: -Display, -GroupBy, -Describe, -Badge and -DisabledReason read each object (a property name, or a scriptblock for choices given with -Choices; loaded choices take property names). For Select fields, -Default @{ Property = 'Online' } selects choices whose Online property is true; -Default also accepts initial values. -CommandValue picks the text the equivalent command shows and the field returns.
Examples
Example 1
$formOptions2 = @{
Title = 'Server selection'
Content = {
$fieldOptions2 = @{
Key = 'Site'
Label = 'Site'
Kind = 'Select'
Choices = @('Warsaw', 'Berlin')
Default = 'Warsaw'
}
New-ConsoleField @fieldOptions2
$fieldOptions = @{
Key = 'Server'
Label = 'Server'
Kind = 'Select'
DependsOn = @('Site')
LoadChoices = {
param($Values)
[pscustomobject]@{ Name = "$($Values.Site)-web01" }
[pscustomobject]@{ Name = "$($Values.Site)-web02" }
}
Display = 'Name'
ParameterName = 'Server'
}
New-ConsoleField @fieldOptions
}
}
$form = New-ConsoleForm @formOptions2
$formOptions = @{
Form = $form
}
Read-ConsoleForm @formOptionsLoad dependent choices from the selected site
Example 2
$formOptions2 = @{
Title = 'Notifications'
Content = {
$fieldOptions2 = @{
Key = 'Notify'
Label = 'Send an email'
Kind = 'Toggle'
}
New-ConsoleField @fieldOptions2
$fieldOptions = @{
Key = 'Email'
Label = 'Email'
VisibleWhen = { $Values.Notify }
Validator = { $_ -match '@' }
}
New-ConsoleField @fieldOptions
}
}
$form = New-ConsoleForm @formOptions2
$formOptions = @{
Form = $form
}
Read-ConsoleForm @formOptionsShow a field only when a toggle is enabled
Example 3
$formOptions2 = @{
Title = 'Servers'
Content = {
$fieldOptions = @{
Multiple = $true
Key = 'Servers'
Label = 'Servers'
Kind = 'Select'
ParameterName = 'ComputerName'
LoadChoices = {
[pscustomobject]@{ Name = 'web01'; Site = 'East'; Role = 'Web'; Offline = ''; Online = $true }
[pscustomobject]@{ Name = 'web02'; Site = 'West'; Role = 'Web'; Offline = 'Maintenance'; Online = $false }
}
Display = 'Name'
CommandValue = 'Name'
GroupBy = 'Site'
Badge = 'Role'
DisabledReason = 'Offline'
Default = @{ Property = 'Online' }
}
New-ConsoleField @fieldOptions
}
}
$form = New-ConsoleForm @formOptions2
$formOptions = @{
Form = $form
}
Read-ConsoleForm @formOptionsGroup loaded choices, disable unavailable ones and select defaults by property
Example 4
$checks = @(
[pscustomobject]@{ Id = 'dns'; Name = 'DNS lookup'; Category = 'Network'; Seconds = 2 }
[pscustomobject]@{ Id = 'disk'; Name = 'Free space'; Category = 'Storage'; Seconds = 1 }
)
$fieldOptions = @{
Multiple = $true
Key = 'Checks'
Label = 'Checks'
Kind = 'Select'
Choices = $checks
Display = 'Name'
CommandValue = 'Id'
GroupBy = 'Category'
Badge = { "~$($_.Seconds) s" }
ParameterName = 'Check'
}
New-ConsoleField @fieldOptionsReturn choice identifiers and show a duration badge
Example 5
Get-ConsoleDemo -Slug 'setup' | Select-Object -ExpandProperty Source
Get-ConsoleDemo -Slug 'toolbox' | Select-Object -ExpandProperty SourceRead any of these gallery scripts to see New-ConsoleField in a complete example. Each lookup returns the script's source. Use the same slug with Show-ConsoleDemo to run it.
Parameters
- -ArgumentList Object[]
- More arguments for the -LoadChoices script.
- -AsSecret SwitchParameter
- For Text: a masked secret, returned as SecureString unless Read-ConsoleForm -AsPlainText.
- -Badge Object
- Short muted text at the right of a choice ("~2 s", "Web"): a property name or a scriptblock. Rich text keeps its own style.
- -Choices Object[]
- The choices of a Select field (any objects).
- -CommandPlaceholder String
- What stands in for a secret in the equivalent command.
- -CommandValue Object
- The value of a choice: the text the equivalent command shows and the field returns, a property name or a scriptblock. The returned text keeps the choice object's properties, so $_.Name still works in -Preview and in the submitted values; presets and remembered choices store the text.
- -Default Object
- The default (for choices: the choice object or its text; for multi-selections: several).
- -DependsOn String[]
- Fields whose change resets this one (and reloads its choices).
- -Describe Object
- A sentence for the focused choice: a property name or a scriptblock. With -LoadChoices, use a property name.
- -DisabledReason Object
- Why a choice cannot be picked: a property name or a scriptblock returning the reason. Nothing, $false or an empty string leaves the choice available; $true disables it without a reason. Disabled choices are dimmed and show the reason while they have focus.
- -Display Object
- What to show for a choice: a property name or a scriptblock.
- -Extension String[]
- File extensions offered as path suggestions.
- -GroupBy Object
- The group of a choice: a property name or a scriptblock.
- -Height Int32
- The visible rows of the text area dialog (Text -Multiline, default 8).
- -Help String
- Help shown while the field has focus.
- -Integer SwitchParameter
- Whole numbers only.
- -Key String
- The key of the value in the result.
- -Kind ConsoleFieldType
- The kind: Text (default), Number, Toggle, Select or Path. -Multiple, -AsSecret and -Multiline choose the variant, as on Read-ConsoleSelect and Read-ConsoleText.
- Possible values:
Text,Number,Toggle,Select,Path - -Label String
- The label (default: the name).
- -LoadChoices ScriptBlock
- Loads the choices in their own runspace; the script gets the current values as $Values (and its first argument).
- -Maximum Decimal
- The largest number, or the most choices of a Select -Multiple field.
- -MaxLength Int32
- The most characters (Text).
- -MaxLines Int32
- The most lines (Text -Multiline).
- -Minimum Decimal
- The smallest number, or the fewest choices of a Select -Multiple field.
- -MinLength Int32
- The fewest characters of a text or secret field.
- -Multiline SwitchParameter
- For Text: several lines, edited in a text area dialog.
- -Multiple SwitchParameter
- For Select: several choices instead of one.
- -MustExist SwitchParameter
- Requires a path to exist.
- -NoRemember SwitchParameter
- Keeps the value out of presets and remembered choices.
- -OffText String
- Text after a toggle that is off (default No).
- -OnText String
- Text after a toggle that is on (default Yes).
- -Page String
- The wizard page the field is on.
- -ParameterName String
- The parameter the field supplies in the equivalent command (Name, -Key or --name).
- -PathKind PathKind
- What a path field accepts: Any, File or Folder.
- Possible values:
Any,File,Folder - -Pattern String
- A pattern a text field must match.
- -PatternMessage String
- The message when -Pattern does not match.
- -Placeholder String
- Muted text while a text field is empty.
- -Preview ScriptBlock
- Preview content for the focused choice in the editor ($_ is the choice).
- -Required SwitchParameter
- Refuses an empty value.
- -Step Decimal
- The step of a number field.
- -Validator ScriptBlock
- A validator: $_ is the value, $Values the other values; return $true (or nothing), $false or an error message.
- -VisibleWhen ScriptBlock
- Shows the field only while this returns $true ($Values holds the other values).
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.