Spotlight on the Label Control

The Spotlight on Controls series describes the controls, that is, the objects in the System.Windows.Forms namespace, so you can use them effectively when building GUI apps in PowerShell Studio and PrimalScript.

Each post focuses on one control and lists its most important properties, methods, and events, including the default event that PowerShell Studio adds to your script when you double-click the control. The posts include many examples written in Windows PowerShell, so you can use them right away.

Read more: Spotlight on Controls: Common properties and methods

This post describes the Label control.

Label Control [System.Windows.Forms.Label]

Creates a clickable text box in a GUI application. Use a label to display text strings.


Tip: To display multiple lines of text, use a Textbox or RichTextBox control.

Important properties of the Label object

Important Events:

Text property: Determines the text in the label

Use the Text property to set, get, or change the text that is displayed on the label.

Value typeSystem.String
Default value: (None)

To set the initial value of Text property, that is, the value that appears when the GUI app starts, use the Properties pane. On the Text row, type the text that appears on the Label.


Screenshot 2016-02-16 16.58.11

NOTE: When you change the value of the Text property of a Label in the Properties pane, PowerShell Studio changes the name of the variable that stores the Label object in your script. For example, if you change the Text property value to ‘Close’, PowerShell Studio changes all references of the Label variable to $LabelClose.

To get or change the label text while the GUI app is running, use the Text property in the event handlers in your script.

To get the text in the label:


To set the text in the label use an assignment statement. You can include variables and expressions in the assigned string value.

$Name = 'PowerShell'
$LabelMain.Text = "Hello, $Name"

To change the text in the label:

$LabelMain_click = {
    # Get the text currently in the label and evaluate it.
    If ($LabelMain.Text -eq 'Click')
        $LabelMain.Text = 'Clicked' # Change the text in the label.
        $LabelMain.Text = 'Click'

Tip: To direct Windows PowerShell to evaluate an expression before inserting the result in a string, use a subexpression. The syntax is $( <expression ).

For example:

Without subexpression:

$LabelMain.Text = "Building automation on (Get-Process 'PowerShell Studio').MainWindowTitle"


With subexpression:

$LabelMain.Text = "Building automation on $((Get-Process 'PowerShell Studio').MainWindowTitle)"


Font property: Determines the style and size of the label text

Use the Font property to get, set, or change the font of the label text.

Value Type: System.Drawing.Font
Default: Microsoft Sans Serif 8.25.

To set the font easily, in the Properties pane for the Label, in the Font row, click the ellipsis (…) and then use the Font selector.



To display the properties of the Font object, in the Properties pane, click the Font group arrow.


To script a font change, set the Font property to a string with the Font name, size, and style.

$labelMain.Font = 'Segoe UI, 15.75pt, style=Bold, Italic'


To determine other properties of the font, create a System.Drawing.Font object.

$ClickFont = [System.Drawing.Font]::new('Microsoft Sans Serif', 8.25, [System.Drawing.FontStyle]::Regular)
$ClickedFont = [System.Drawing.Font]::new('Segoe UI', 16, [System.Drawing.FontStyle]::Italic)
    if ($labelMain.Text -eq 'Click')
        $labelMain.Text = 'Clicked'
        $labelMain.Font = $ClickedFont
        $labelMain.Text = 'Click'
        $labelMain.Font = $ClickFont


  • To enlarge the label automatically to fit its contents, set the AutoSize property of the Label to $True.
  • When scripting a font, be sure to set the Font property of the Label. The Text property of the Label does not have a Font property.

ForeColor property: Determines the color of the label text.

Use the ForeColor property to get, set, and change the color of the text in the label.

Values: Any [System.Drawing.Color] type
Default: ControlText (Black)

To set the ForeColor property in the Properties pane.



To set ForeColor property in a script:

$Label1.ForeColor = [System.Drawing.Color]::Red


$Label1.ForeColor = "Red"

BackColor property: Determines the color of the background in the label

Use the BackColor property to get, set, and change the color of the label background.

Value Type: [System.Drawing.Color]
Default: (Determined by the operating system. Typically ‘Control’)

To set the BackColor property in a script:

If ($Label1.BackColor -eq 'Control')
    $Label1.BackColor = [System.Drawing.Color]::ControlDark


$Label1.BackColor = "ControlDark"


TextAlign property: Determines the position of the text in the label

Use the TextAlign property to get, set, or change the alignment of the text relative to the label border.

Value Type: [System.Drawing.ContentAlignment] enumeration
Values: TopLeft, TopCenter, TopRight, MiddleLeft, MiddleCenter, MiddleRight, BottomLeft, BottomCenter, BottomRight
Default: MiddleCenter
To set the TextAlign property in the Property pane:

To set the TextAlign property in a script:

$label1.TextAlign = 'TopRight'

BorderStyle property: Determines the style and thickness of the label border.

Use the BorderStyle property to get, set, or change the label border. Label borders are fixed. The end-user cannot resize any label border style.

Value Type: [System.Windows.Forms.BorderStyle] enumerator.
Values: None, FixedSingle, Fixed3D
Default: None

$label1.BorderStyle = 'None'


$label1.BorderStyle = [System.Windows.Forms.BorderStyle]'None'


$label1.BorderStyle = 'FixedSingle'


$label1.BorderStyle = 'Fixed3D'


Image property: Determines that image that appears on the label

Use this property to display an icon, bitmap, or image of any type on the label.

Value Type: [System.Drawing.Image]
Default: (None; Same as Form.BackgroundImage)

To add an image from a file to a label, use Image property in the Property pane. Click the ellipsis and, in File Explorer, select a file. PowerShell Studio converts the file into a bitmap string (64-bit byte array) so it is independent of the local system.


On the label:


Tip: If you are using a label to display an image without text, consider using a PictureBox object.

Note: To add an image to a label, PowerShell Studio converts the image to a 64-bit string and then uses the FromBase64String static method to pass a byte array as the System.Drawing.Image type. It does not pass a path or use any local system values.

Enabled property: Determines whether the label can be clicked

Use the Enabled property to temporarily enable or disable the click feature of the label. For example, use Enabled = $false to disable the click feature after the label has been clicked or while a function is running.

Value Type: [System.Boolean] ($True, $False).
Default: $True

To completely disable the click feature of the label, omit the Click event handler or enter an empty script block.

To set the Enabled property in the script:

$Label1.Enabled = $false

For example:

If ($Label1.Text -eq 'Click')
    $Label1.Text -eq 'Clicked'
    $Label1.Enabled = $false

Click event: Determines how the label responds when a user clicks it.

The event handler for the Click event is called when the label is clicked. Use the Click property to trigger a script block when a user clicks on the button.

    $label1.Enabled = $false
    $label1.Text = "Disabled"

Before Click event:


After Click event:



Note about events:

To discover which control called the event script block, use the $this automatic variable. The $this variable is particularly useful when you have multiple controls calling the same event script block. The $this variable tells which control triggered the event, so you can respond in a control-specific way.

For example, this Click event script block disables the control that called it:

    $this.Enabled = $false

Looking for more Spotlight posts? In PowerShell Studio, in the Toolbox or in the Properties pane, right-click a control and then click ‘View Spotlight Article’.