We are no longer accepting contributions to Documentation. Please see our post on meta.

PowerShell

GUI in Powershell All Versions

1.0
2.0
3.0
4.0
5.0
5.1

This draft deletes the entire topic.

Examples

  • Improvements requested:

    • This example is completely unclear, incomplete, or has severe formatting problems. It is unlikely to be salvageable through editing and should be removed. –  Moerwald Feb 9 at 6:06
      Would be cool to have some additional explanation what the posted code does.
      1. The edits seem to have addressed your concerns. The code works as posted and has comments that explain what the various (large) pieces of code do.

    We are no longer accepting contributions to Documentation. Improvement requests can no longer be handled.

    2
    Add-Type -AssemblyName PresentationFramework
     
    [xml]$XAMLWindow = '
    <Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="Auto"
        SizeToContent="WidthAndHeight"
        Title="Get-Service">
        <ScrollViewer Padding="10,10,10,0" ScrollViewer.VerticalScrollBarVisibility="Disabled">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Margin="10,10,0,10">ComputerName:</Label>
                    <TextBox Name="Input" Margin="10" Width="250px"></TextBox>
                </StackPanel>
                <DockPanel>
                    <Button Name="ButtonGetService" Content="Get-Service" Margin="10" Width="150px" IsEnabled="false"/>
                    <Button Name="ButtonClose" Content="Close" HorizontalAlignment="Right" Margin="10" Width="50px"/>
                </DockPanel>
            </StackPanel> 
        </ScrollViewer >
    </Window>
    '
    
    # Create the Window Object
    $Reader=(New-Object System.Xml.XmlNodeReader $XAMLWindow)
    $Window=[Windows.Markup.XamlReader]::Load( $Reader )
    
    # TextChanged Event Handler for Input 
    $TextboxInput = $Window.FindName("Input")
    $TextboxInput.add_TextChanged.Invoke({
        $ComputerName = $TextboxInput.Text
        $ButtonGetService.IsEnabled = $ComputerName -ne ''
    })
    
    # Click Event Handler for ButtonClose
    $ButtonClose = $Window.FindName("ButtonClose")
    $ButtonClose.add_Click.Invoke({
        $Window.Close();
    })
    
    # Click Event Handler for ButtonGetService
    $ButtonGetService = $Window.FindName("ButtonGetService")
    $ButtonGetService.add_Click.Invoke({
        $ComputerName = $TextboxInput.text.Trim()
        try{
            Get-Service -ComputerName $computerName | Out-GridView -Title "Get-Service on $ComputerName"
        }catch{
            [System.Windows.MessageBox]::Show($_.exception.message,"Error",[System.Windows.MessageBoxButton]::OK,[System.Windows.MessageBoxImage]::Error)
        }
    })
    
    # Open the Window
    $Window.ShowDialog() | Out-Null
    

    This creates a dialog window which allows the user to select a computer name, then will display a table of services and their statuses on that computer.
    This example uses WPF rather than Windows Forms.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about GUI in Powershell? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.