Is it possible to color grep output in powershell?
I would like to run a java application on windows but color some output lines different background and foreground colors based upon matching text.
Is this possible with Windows powershell? How would I go about doing that?
powershell colors
add a comment |
I would like to run a java application on windows but color some output lines different background and foreground colors based upon matching text.
Is this possible with Windows powershell? How would I go about doing that?
powershell colors
add a comment |
I would like to run a java application on windows but color some output lines different background and foreground colors based upon matching text.
Is this possible with Windows powershell? How would I go about doing that?
powershell colors
I would like to run a java application on windows but color some output lines different background and foreground colors based upon matching text.
Is this possible with Windows powershell? How would I go about doing that?
powershell colors
powershell colors
edited Dec 11 at 11:49
not2qubit
8351122
8351122
asked Jun 14 '17 at 9:06
Mikey
1,00021624
1,00021624
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
As in my simple example below you could try matching your output and colouring accordingly.
$Items = @("Find","Matching","Item")
$colItem = "Matching"
foreach ($i in $Items) {
if ($i -match $colItem){
write-host $i -foregroundcolor magenta -BackgroundColor yellow}
else {write-host $i}
}
--Edit--
Taking it further with a rough working example (only checked with ps4) "grepping" the Get Help cmdlet output for the Phrase PowerShell. will give coloured ouput for the first Phrase per line
Function Coloured-Output {
Process {
$i = "PowerShell"
If ($_ -match $i){
$iPosition = $_.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $_.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
Write-Host $_.Substring(0,$iPosition) -NoNewline
Write-Host $_.Substring($iPosition,$iLength) -Foregroundcolor Blue -BackgroundColor cyan -NoNewline
Write-Host $_.Substring($iEnd,$iComplete)
}
else {write-host $_ }
} # End of Process
} # End of Function
$SplitThis = Get-Help
$SplitThis -split ("`n") | Out-String -stream | Coloured-Output
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
add a comment |
The Short answer is simply no.
The Long answer is that anything is possible, but ...
Unfortunately any coloring and string operations in PowerShell is a royal PITA. The reason is that the out-of-the-box Windows console terminals are using their own way to color in/out, and not using escape sequences for coloring.* [See note!] This (AFAIK) always require using Write-Host
. So any color operations soon become counter productive as they often require extensive Windows engineering, to work around too early output and not being able to easily store colors in a string variable.
Either way, here is a more useful solution for highlighting unsafe ExecutionPolicy
settings. (Basically a working modification of Antony's answer.) Here it is using a preset search criteria (as an array), but could probably be modified and converted to a proper ColorGrep(<string>)
function.
# To get the ExecutionPolicy List
Function Get-MyPolicy {
$ZZ_EPOL = ((Get-ExecutionPolicy -List) | Format-Table -hideTableHeader @{Label="ExePol"; e={" {0,-16}: {1,-20}" -f $_.Scope, $_.ExecutionPolicy}})
$ZZ_EPOL
}
# Colorize the unsafe Execution Policies
Function ColorMatch {
Process {
$polkeys = @("Bypass","Unrestricted")
foreach ($line in $_) {
foreach ($i in $polkeys) {
$res =''
If ($line -match $i) {
$iPosition = $line.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $line.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
$res = (Write-Host $line.Substring(0, $iPosition) -NoNewline)
$res += (Write-Host $line.Substring($iPosition, $iLength) -ForegroundColor Red -NoNewline)
$res += (Write-Host $line.Substring($iEnd, $iComplete) -NoNewline)
Write-Host $res
break # There's only one match per line
}
} # END foreach 2
If (($res -eq '') -and ($line -ne '')) {
Write-Host $line
}
} # END foreach 1
} # END process
}
To run this, use:Get-MyPolicy | Out-String -stream | ColorMatch
The output is:
Finally, if you need to pass in other strings, you may need to parse the $line
and $_
input to separate the strings into lines. Since the script is assuming that there is only one match per line. That is why the Out-String
is used.
NOTE:
Recently Windows 10
has added some color capability. However, there are dozens of 3-rd party console solutions that already does this.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1219110%2fis-it-possible-to-color-grep-output-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As in my simple example below you could try matching your output and colouring accordingly.
$Items = @("Find","Matching","Item")
$colItem = "Matching"
foreach ($i in $Items) {
if ($i -match $colItem){
write-host $i -foregroundcolor magenta -BackgroundColor yellow}
else {write-host $i}
}
--Edit--
Taking it further with a rough working example (only checked with ps4) "grepping" the Get Help cmdlet output for the Phrase PowerShell. will give coloured ouput for the first Phrase per line
Function Coloured-Output {
Process {
$i = "PowerShell"
If ($_ -match $i){
$iPosition = $_.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $_.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
Write-Host $_.Substring(0,$iPosition) -NoNewline
Write-Host $_.Substring($iPosition,$iLength) -Foregroundcolor Blue -BackgroundColor cyan -NoNewline
Write-Host $_.Substring($iEnd,$iComplete)
}
else {write-host $_ }
} # End of Process
} # End of Function
$SplitThis = Get-Help
$SplitThis -split ("`n") | Out-String -stream | Coloured-Output
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
add a comment |
As in my simple example below you could try matching your output and colouring accordingly.
$Items = @("Find","Matching","Item")
$colItem = "Matching"
foreach ($i in $Items) {
if ($i -match $colItem){
write-host $i -foregroundcolor magenta -BackgroundColor yellow}
else {write-host $i}
}
--Edit--
Taking it further with a rough working example (only checked with ps4) "grepping" the Get Help cmdlet output for the Phrase PowerShell. will give coloured ouput for the first Phrase per line
Function Coloured-Output {
Process {
$i = "PowerShell"
If ($_ -match $i){
$iPosition = $_.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $_.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
Write-Host $_.Substring(0,$iPosition) -NoNewline
Write-Host $_.Substring($iPosition,$iLength) -Foregroundcolor Blue -BackgroundColor cyan -NoNewline
Write-Host $_.Substring($iEnd,$iComplete)
}
else {write-host $_ }
} # End of Process
} # End of Function
$SplitThis = Get-Help
$SplitThis -split ("`n") | Out-String -stream | Coloured-Output
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
add a comment |
As in my simple example below you could try matching your output and colouring accordingly.
$Items = @("Find","Matching","Item")
$colItem = "Matching"
foreach ($i in $Items) {
if ($i -match $colItem){
write-host $i -foregroundcolor magenta -BackgroundColor yellow}
else {write-host $i}
}
--Edit--
Taking it further with a rough working example (only checked with ps4) "grepping" the Get Help cmdlet output for the Phrase PowerShell. will give coloured ouput for the first Phrase per line
Function Coloured-Output {
Process {
$i = "PowerShell"
If ($_ -match $i){
$iPosition = $_.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $_.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
Write-Host $_.Substring(0,$iPosition) -NoNewline
Write-Host $_.Substring($iPosition,$iLength) -Foregroundcolor Blue -BackgroundColor cyan -NoNewline
Write-Host $_.Substring($iEnd,$iComplete)
}
else {write-host $_ }
} # End of Process
} # End of Function
$SplitThis = Get-Help
$SplitThis -split ("`n") | Out-String -stream | Coloured-Output
As in my simple example below you could try matching your output and colouring accordingly.
$Items = @("Find","Matching","Item")
$colItem = "Matching"
foreach ($i in $Items) {
if ($i -match $colItem){
write-host $i -foregroundcolor magenta -BackgroundColor yellow}
else {write-host $i}
}
--Edit--
Taking it further with a rough working example (only checked with ps4) "grepping" the Get Help cmdlet output for the Phrase PowerShell. will give coloured ouput for the first Phrase per line
Function Coloured-Output {
Process {
$i = "PowerShell"
If ($_ -match $i){
$iPosition = $_.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $_.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
Write-Host $_.Substring(0,$iPosition) -NoNewline
Write-Host $_.Substring($iPosition,$iLength) -Foregroundcolor Blue -BackgroundColor cyan -NoNewline
Write-Host $_.Substring($iEnd,$iComplete)
}
else {write-host $_ }
} # End of Process
} # End of Function
$SplitThis = Get-Help
$SplitThis -split ("`n") | Out-String -stream | Coloured-Output
edited Dec 11 at 10:42
answered Jun 14 '17 at 15:58
Antony
971912
971912
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
add a comment |
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
Unfortunately, that function only matches the first occurrence of "PowerShell".
– not2qubit
Dec 10 at 15:54
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
@not2qubit added missing split for each line, now just first occurrence per line.
– Antony
Dec 11 at 11:21
add a comment |
The Short answer is simply no.
The Long answer is that anything is possible, but ...
Unfortunately any coloring and string operations in PowerShell is a royal PITA. The reason is that the out-of-the-box Windows console terminals are using their own way to color in/out, and not using escape sequences for coloring.* [See note!] This (AFAIK) always require using Write-Host
. So any color operations soon become counter productive as they often require extensive Windows engineering, to work around too early output and not being able to easily store colors in a string variable.
Either way, here is a more useful solution for highlighting unsafe ExecutionPolicy
settings. (Basically a working modification of Antony's answer.) Here it is using a preset search criteria (as an array), but could probably be modified and converted to a proper ColorGrep(<string>)
function.
# To get the ExecutionPolicy List
Function Get-MyPolicy {
$ZZ_EPOL = ((Get-ExecutionPolicy -List) | Format-Table -hideTableHeader @{Label="ExePol"; e={" {0,-16}: {1,-20}" -f $_.Scope, $_.ExecutionPolicy}})
$ZZ_EPOL
}
# Colorize the unsafe Execution Policies
Function ColorMatch {
Process {
$polkeys = @("Bypass","Unrestricted")
foreach ($line in $_) {
foreach ($i in $polkeys) {
$res =''
If ($line -match $i) {
$iPosition = $line.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $line.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
$res = (Write-Host $line.Substring(0, $iPosition) -NoNewline)
$res += (Write-Host $line.Substring($iPosition, $iLength) -ForegroundColor Red -NoNewline)
$res += (Write-Host $line.Substring($iEnd, $iComplete) -NoNewline)
Write-Host $res
break # There's only one match per line
}
} # END foreach 2
If (($res -eq '') -and ($line -ne '')) {
Write-Host $line
}
} # END foreach 1
} # END process
}
To run this, use:Get-MyPolicy | Out-String -stream | ColorMatch
The output is:
Finally, if you need to pass in other strings, you may need to parse the $line
and $_
input to separate the strings into lines. Since the script is assuming that there is only one match per line. That is why the Out-String
is used.
NOTE:
Recently Windows 10
has added some color capability. However, there are dozens of 3-rd party console solutions that already does this.
add a comment |
The Short answer is simply no.
The Long answer is that anything is possible, but ...
Unfortunately any coloring and string operations in PowerShell is a royal PITA. The reason is that the out-of-the-box Windows console terminals are using their own way to color in/out, and not using escape sequences for coloring.* [See note!] This (AFAIK) always require using Write-Host
. So any color operations soon become counter productive as they often require extensive Windows engineering, to work around too early output and not being able to easily store colors in a string variable.
Either way, here is a more useful solution for highlighting unsafe ExecutionPolicy
settings. (Basically a working modification of Antony's answer.) Here it is using a preset search criteria (as an array), but could probably be modified and converted to a proper ColorGrep(<string>)
function.
# To get the ExecutionPolicy List
Function Get-MyPolicy {
$ZZ_EPOL = ((Get-ExecutionPolicy -List) | Format-Table -hideTableHeader @{Label="ExePol"; e={" {0,-16}: {1,-20}" -f $_.Scope, $_.ExecutionPolicy}})
$ZZ_EPOL
}
# Colorize the unsafe Execution Policies
Function ColorMatch {
Process {
$polkeys = @("Bypass","Unrestricted")
foreach ($line in $_) {
foreach ($i in $polkeys) {
$res =''
If ($line -match $i) {
$iPosition = $line.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $line.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
$res = (Write-Host $line.Substring(0, $iPosition) -NoNewline)
$res += (Write-Host $line.Substring($iPosition, $iLength) -ForegroundColor Red -NoNewline)
$res += (Write-Host $line.Substring($iEnd, $iComplete) -NoNewline)
Write-Host $res
break # There's only one match per line
}
} # END foreach 2
If (($res -eq '') -and ($line -ne '')) {
Write-Host $line
}
} # END foreach 1
} # END process
}
To run this, use:Get-MyPolicy | Out-String -stream | ColorMatch
The output is:
Finally, if you need to pass in other strings, you may need to parse the $line
and $_
input to separate the strings into lines. Since the script is assuming that there is only one match per line. That is why the Out-String
is used.
NOTE:
Recently Windows 10
has added some color capability. However, there are dozens of 3-rd party console solutions that already does this.
add a comment |
The Short answer is simply no.
The Long answer is that anything is possible, but ...
Unfortunately any coloring and string operations in PowerShell is a royal PITA. The reason is that the out-of-the-box Windows console terminals are using their own way to color in/out, and not using escape sequences for coloring.* [See note!] This (AFAIK) always require using Write-Host
. So any color operations soon become counter productive as they often require extensive Windows engineering, to work around too early output and not being able to easily store colors in a string variable.
Either way, here is a more useful solution for highlighting unsafe ExecutionPolicy
settings. (Basically a working modification of Antony's answer.) Here it is using a preset search criteria (as an array), but could probably be modified and converted to a proper ColorGrep(<string>)
function.
# To get the ExecutionPolicy List
Function Get-MyPolicy {
$ZZ_EPOL = ((Get-ExecutionPolicy -List) | Format-Table -hideTableHeader @{Label="ExePol"; e={" {0,-16}: {1,-20}" -f $_.Scope, $_.ExecutionPolicy}})
$ZZ_EPOL
}
# Colorize the unsafe Execution Policies
Function ColorMatch {
Process {
$polkeys = @("Bypass","Unrestricted")
foreach ($line in $_) {
foreach ($i in $polkeys) {
$res =''
If ($line -match $i) {
$iPosition = $line.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $line.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
$res = (Write-Host $line.Substring(0, $iPosition) -NoNewline)
$res += (Write-Host $line.Substring($iPosition, $iLength) -ForegroundColor Red -NoNewline)
$res += (Write-Host $line.Substring($iEnd, $iComplete) -NoNewline)
Write-Host $res
break # There's only one match per line
}
} # END foreach 2
If (($res -eq '') -and ($line -ne '')) {
Write-Host $line
}
} # END foreach 1
} # END process
}
To run this, use:Get-MyPolicy | Out-String -stream | ColorMatch
The output is:
Finally, if you need to pass in other strings, you may need to parse the $line
and $_
input to separate the strings into lines. Since the script is assuming that there is only one match per line. That is why the Out-String
is used.
NOTE:
Recently Windows 10
has added some color capability. However, there are dozens of 3-rd party console solutions that already does this.
The Short answer is simply no.
The Long answer is that anything is possible, but ...
Unfortunately any coloring and string operations in PowerShell is a royal PITA. The reason is that the out-of-the-box Windows console terminals are using their own way to color in/out, and not using escape sequences for coloring.* [See note!] This (AFAIK) always require using Write-Host
. So any color operations soon become counter productive as they often require extensive Windows engineering, to work around too early output and not being able to easily store colors in a string variable.
Either way, here is a more useful solution for highlighting unsafe ExecutionPolicy
settings. (Basically a working modification of Antony's answer.) Here it is using a preset search criteria (as an array), but could probably be modified and converted to a proper ColorGrep(<string>)
function.
# To get the ExecutionPolicy List
Function Get-MyPolicy {
$ZZ_EPOL = ((Get-ExecutionPolicy -List) | Format-Table -hideTableHeader @{Label="ExePol"; e={" {0,-16}: {1,-20}" -f $_.Scope, $_.ExecutionPolicy}})
$ZZ_EPOL
}
# Colorize the unsafe Execution Policies
Function ColorMatch {
Process {
$polkeys = @("Bypass","Unrestricted")
foreach ($line in $_) {
foreach ($i in $polkeys) {
$res =''
If ($line -match $i) {
$iPosition = $line.IndexOf($i) # start position of "grep phrase"
$iLength = $i.Length # length of grep phrase
$iEnd = $iPosition + $iLength # end of grep phrase
$LineLength = $line.Length # length of line
$iComplete = $LineLength - $iEnd # length of characters to complete the line
$res = (Write-Host $line.Substring(0, $iPosition) -NoNewline)
$res += (Write-Host $line.Substring($iPosition, $iLength) -ForegroundColor Red -NoNewline)
$res += (Write-Host $line.Substring($iEnd, $iComplete) -NoNewline)
Write-Host $res
break # There's only one match per line
}
} # END foreach 2
If (($res -eq '') -and ($line -ne '')) {
Write-Host $line
}
} # END foreach 1
} # END process
}
To run this, use:Get-MyPolicy | Out-String -stream | ColorMatch
The output is:
Finally, if you need to pass in other strings, you may need to parse the $line
and $_
input to separate the strings into lines. Since the script is assuming that there is only one match per line. That is why the Out-String
is used.
NOTE:
Recently Windows 10
has added some color capability. However, there are dozens of 3-rd party console solutions that already does this.
edited Dec 11 at 11:19
answered Dec 11 at 10:03
not2qubit
8351122
8351122
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1219110%2fis-it-possible-to-color-grep-output-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown