trying to array slice the get-psreadlineoption command. I want to select all strings containing the word 'Color' and then input those to 'set-psreadline' to set all colors. I do not understand how this all works in powershell. It's an array, right? I can just loop over it like this:
foreach($a in $i) { $i; if ($i -contains 'MemberColor') {$i;} }
But that gives no output. I also cannot access it as an array:
get-psreadlineoption[21..36]
get-psreadlineoption['EditMode']
Does not work. Basically what I want to do is:
foreach (get-psreadlines[21..36]) { $array = append(<string that contains 'Color'>) } foreach ($a in $array) { set-psreadline($a, ...)}
How would I go about doing this?
(P.S. I just want to set all those colors to the same color in a concise manner as possible)
CodePudding user response:
To get the names of all properties containing the substring color (case-insensitively), use reflection, which PowerShell facilitates via the intrinsic .psobject property:
(Get-PSReadLineOption).psobject.Properties.Name -like '*color*'
Note: A quick way to print all property names of interest, including their values, is to use
Get-PSReadLineOption | Select-Object *color*. However, the result is a [pscustomobject], not a hashtable, and in order to convert the former to the latter you'd again need the same technique shown below.
To create a hashtable suitable for passing to Set-PSReadLineOption based on these properties, using the current property values:
$colors = @{}
(Get-PSReadLineOption).psobject.Properties.
Where({ $_.Name -like '*color*' }).
ForEach({ $colors[$_.Name] = $_.Value })
Note: When you print $colors, the Value column may appear to be empty, but the values are there, in the form of - themselves invisible - Virtual Terminal (VT) control sequences.
CodePudding user response:
This is sort of a common question, how to loop through properties Iterate over PSObject properties in PowerShell. You can also use get-member to get the property list. You can also pipe to "select *color".
Get-PSReadLineOption | get-member *color | % name
CommandColor
CommentColor
ContinuationPromptColor
DefaultTokenColor
EmphasisColor
ErrorColor
KeywordColor
MemberColor
NumberColor
OperatorColor
ParameterColor
PredictionColor
SelectionColor
StringColor
TypeColor
VariableColor
