Describes the available support for ANSI escape sequences in PSTree Module.
PSTree v2.2.0 adds support for coloring the hierarchy output from Get-PSTree
cmdlet via the TreeStyle
type. The type offers a subset of capabilities that the built-in PSStyle
has, more specifically, the FileInfoFormatting
subset.
The instance of this type can be accessed via the Get-PSTreeStyle
cmdlet or the [PSTree.Style.TreeStyle]::Instance
property:
It has some useful methods to combine escape sequences as well as add accents, see next section for more details.
TypeName: PSTree.Style.TreeStyle
Name MemberType Definition
---- ---------- ----------
CombineSequence Method string CombineSequence(string left, string right)
Equals Method bool Equals(System.Object obj)
EscapeSequence Method string EscapeSequence(string vt)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ResetSettings Method void ResetSettings()
ToBold Method string ToBold(string vt)
ToItalic Method string ToItalic(string vt)
ToString Method string ToString()
Directory Property string Directory {get;set;}
Executable Property string Executable {get;set;}
Extension Property PSTree.Style.Extension Extension {get;}
OutputRendering Property PSTree.Style.OutputRendering OutputRendering {get;set;}
Palette Property PSTree.Style.Palette Palette {get;}
Reset Property string Reset {get;}
The EscapeSequence()
method can be used to see the escape sequence used to produce the color and accent, for example:
Similar to PSStyle
you can update the properties of TreeStyle
as well as add an remove coloring for different extensions.
Note
- For now, customizing the output of files that are a SymbolicLink is not supported.
- The Executable accent is only available for Windows Operating System.
For example, take the standard output:
We can make a few changes to the style object:
$style = Get-PSTreeStyle
$palette = $style.Palette
# update the .ps1 extension
$style.Extension['.ps1'] = $style.CombineSequence($palette.Foreground.White, $palette.Background.Red)
# add the .cs extension
$style.Extension['.cs'] = $style.ToItalic($style.ToBold($palette.ForeGround.BrightCyan))
# update the Directory style
$style.Directory = "`e[45m"
Tip
- The
`e
escape character was added in PowerShell 6. Windows PowerShell 5.1 users can use[char] 27
instead, for example from previous example, instead of"`e[45m"
you can use"$([char] 27)[45m"
. See about_Special_Characters for more details. - The
TreeStyle
type has 3 public methods that you can use to add accents or combine VT sequences,ToItalic()
,ToBold()
andCombineSequence()
. - You can also reset the style instance to its initial state using
.ResetSettings()
however if you had the instance stored in a variable you will need to re-assign its value, i.e.:$style.ResetSettings()
then$style = treestyle
.
Then, if we re-run the same command we can see those changes in the PSTree output:
Similarly to PSStyle
, you can disable the ANSI rendering by updating the OutputRendering
property:
(Get-PSTreeStyle).OutputRendering = 'PlainText'