Real-time WPF GUI Updates in PowerShell Script Not Working

Swahela Mulla 90 Reputation points

Hello Everyone,

I'm facing an issue with my PowerShell script where I'm trying to dynamically update a TextBlock/Label in my WPF GUI to reflect different stages of a process. Unfortunately, I'm only seeing the default value "Not Started" and the final value "Completed!" being displayed in my GUI (All process we able to see in PowerShell Console but not in GUI). I've tried a few different approaches including Dispatcher.Invoke, UpdateLabel.Invoke, and even using runspaces, but nothing seems to work as expected.

I've tested the script in my environment and even tried the suggested solutions from online resources, but still haven't achieved the desired real-time updates. Here's a simplified version of my script:

Add-Type -AssemblyName PresentationFramework,PresentationCore,System.Windows.Forms

if (Get-Variable "myhash" -Scope Global -ErrorAction SilentlyContinue){Remove-Variable -Name "myHash" -ErrorAction SilentlyContinue;$Error.Clear()}Else{$Error.Clear()}
$Global:myHash = [hashtable]::Synchronized(@{})

$myHash.wpfButton1_Click= {

    $myHash.WPFLabel_2.Content = "Running Script..."
    Write-Host "Starting to Sleep - Running Script"
    Sleep -Seconds 5

    $myHash.WPFLabel_2.Content = "Gatharing Data.."
    Write-Host "Starting to Sleep - Gatharing Data.."
    Sleep -Seconds 5

    $myHash.WPFLabel_2.Content = "Processing Data.."
    Write-Host "Starting to Sleep - Processing Data.."
    Sleep -Seconds 5
    $myHash.WPFLabel_2.Content = "Completed!"
    Write-Host "Completed."  

[xml]$myHash.myXAML = '<Window 
        Height="450" Width="450"
        ShowInTaskbar ="True"

         <Grid ShowGridLines="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <!-- This is the main grid on the form. It divides the form into 2 rows. Top row is contains the header and logo. Bottom row contains the message -->
			    <ColumnDefinition Width="*"/>
			    <RowDefinition Height="*" />

            <StackPanel Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Orientation ="Horizontal" >
                <Label Name="WPFLabel_1" Margin="2,0,0,0" > Run ME :  </Label>
                <Label Name="WPFLabel_2" Margin="2,0,0,0" > Not Started  </Label>
                <Button Name="wpfButton1" Content="Start Script" Margin="2,0,0,0" Height="25" Width="100" VerticalAlignment="Top"/>

    $myHash.myForm = [Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $myHash.myXAML))

    $myHash.myXAML.SelectNodes("//*[@Name]") | ForEach-Object {if (($_.Name).length -gt 1){$myHash.Add($_.Name, $myHash.myForm.FindName($_.Name))} }



I've exhausted my troubleshooting efforts and would appreciate any guidance or suggestions from the community. If anyone has successfully achieved real-time GUI updates in a WPF PowerShell script, could you kindly share your approach and any tips that might help me resolve this issue?

Thank you in advance for your assistance!

Best regards,


Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,709 questions
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Muhammad Binyameen 395 Reputation points

    I'd be happy to help you with the issue you're facing. Could you provide me with more information about the specific problem you're encountering with real-time WPF GUI updates in your PowerShell script? The more details you can provide, the better I'll be able to assist you.