1

I am using a Powershell form to output some data and I am wondering how I can get the output in color?

I am not using write-host. That is not what I am looking for. I know you can use -ForegroundColor for that.

It's for Get-ADUser -Filter "UserPrincipalName -like 'Username'" | Select Enabled

If output is False it needs to be in Red. If output is true just regular color.

Anyone who can help me?

Many thanks. Ralph.

6
  • 1
    Color is part of the user interface. Objects, like the ones being output by your command, don't have colors, otherwise. Why do you say Write-Host "is not what I am looking for"? If you want to customize the object display with colors then Write-Host is the way to do it. Commented May 14, 2020 at 18:09
  • It's because I need to use it in a Powershell Form. Then I can't use Write-Host... Commented May 14, 2020 at 18:11
  • what type of form is it? xaml or the older one? Commented May 14, 2020 at 18:13
  • 1
    I see. I missed the word "form" in the question. Presumably you mean PowerShell instantiating WinForms controls? It'd be helpful to show the code of this form and how it loads the data from that command. Commented May 14, 2020 at 18:15
  • @Ralph can you post the code for the form component that's supposed to show the colored output? Commented May 14, 2020 at 19:35

1 Answer 1

2

A follow-up to my comment

#region Begin functions and code behind

function RunCode { 
    $ProcessList = (Get-Process).Name
    If ($ProcessList -ge 10)
    {$DataSet.ForeColor = 'red'}
    else {$DataSet.ForeColor = 'black'}
    [void] $DataSet.Items.Addrange($ProcessList)
}

#endregion End functions and code behind


#region Begin GUI code
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '511,501'
$Form.text                       = "Form"
$Form.TopMost                    = $false

$RunCode                         = New-Object system.Windows.Forms.Button
$RunCode.text                    = "RunCode"
$RunCode.width                   = 90
$RunCode.height                  = 30
$RunCode.location                = New-Object System.Drawing.Point(19,17)
$RunCode.Font                    = 'Microsoft Sans Serif,10'

$DataSet                         = New-Object system.Windows.Forms.ListBox
$DataSet.text                    = "listBox"
$DataSet.width                   = 204
$DataSet.height                  = 144
$DataSet.location                = New-Object System.Drawing.Point(17,98)

$Form.controls.AddRange(@(
    $RunCode,
    $DataSet
))

$RunCode.Add_Click({ RunCode })

#endregion Begin GUI code


# Call the GUI
[void]$Form.ShowDialog()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.