If you're using VS Code (I can't speak for Visual Studio because I rarely use it) and set a breakpoint on line 33 of your script ($Updated = "YES") and whan you examine "Variables" pane, you'll find there are two $Updated variables. One with a Script scope and the other with a Global scope.
Use an explicit scope when declaring and referencing that variable:
$Script:Updated = "NO"
#region iS THE SYSTEM UPDATED?
Add-Type -Assembly PresentationFramework
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray">
<StackPanel x:Name='StackPanel'>
<TextBox x:Name='textbox' Text='Has the machine been completely Updated?' IsReadOnly="True" />
<RadioButton x:Name="Item1" Content = 'YES' Foreground='Green'/>
<RadioButton x:Name="Item2" Content = 'NO' Foreground='RED'/>
</StackPanel>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$zzz = $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")
ForEach ($z in $zzz)
{
Set-Variable -Name ($z.Name) -Value $Window.FindName($z.Name)
}
[System.Windows.RoutedEventHandler]$Script:CheckedEventHandler =
{
Write-Host "changed"
if ( $_.source.Content.Equals( "YES" ) )
{
$Script:Updated = "YES"
}
else
{
$Script:Updated = "NO"
}
Write-Host $Script:Updated
}
$StackPanel.AddHandler([System.Windows.Controls.RadioButton]::CheckedEvent, $CheckedEventHandler)
$Window.Showdialog() | Out-Null
Write-Host $Script:Updated
if ( $Script:Updated.Equals("NO") )
{
Write-Host "Exiting"
Exit
}
Write-Host "Carry On"