Poor Man’s GUI

Poor Man’s GUI

If you’re like me, you try to automate everything through the PowerShell console. Automate employee account creation, easy. Check the registry on 500 remote servers, wouldn’t break a sweat. How about delegating your script to a junior systems administrator or maybe a help desk technician? That isn’t so easy. You have to trap errors, sanitize inputs, and generally guide the user through documentation. After all that, trust that you’re underling knows their way around the console. Maybe you need a GUI.

Sometimes it is easier to break with the PowerShell conventions and introduce some user interaction into the process. With that you can use Read-Host, Out-GridView, and Show-Command.

Read-Host

Read-Host is meant to handle text input from the console at run-time. It can be useful for specifying server names in a script or adding a filter to a search.

$ProcessToKill = Read-Host -Prompt 'What process should we kill?'

Stop-Process -Name $processtokill

The drawback to this is there is usually a better way to input text in the console. If you’re using Read-Host for usernames and passwords you’re better off with Get-Credential. If you’re running a cmdlet that requires input, the console will prompt you for it.

filter prompt

In other words, Read-Host can only bring you so far. Time to mimic GUI applications in the console.

Out-GridView

Out-GridView offers up a picker for quick and dirty tasks like killing processes. The following code will let you select one process to stop.

Get-Process | Out-GridView -Title 'Process to Kill' -OutputMode Single | Stop-Process

picker gui

Pressing the OK button will end the notepad.exe object down the pipeline to Stop-Process.

This is also useful for removing Active Directory accounts for those who are used to a GUI application. Get all users, select your victims, and remove their accounts.

Get-ADUser -Filter * | Out-GridView -Title 'Users to Disable' -OutputMode Multiple | Remove-ADUser -confirm:$false

With the -OuputMode Multiple you can select any number of accounts to remove.

Show-Command

Show-Command allows for a GUI with built-in help button.

Function Write-Screen {
    param (
        $Sender,
        $Message
    )

    $WriteHost = "[{0}]: {1}" -f $Sender, $Message
    Clear-Host
    Write-Host $WriteHost
}
Show-Command -NoCommonParameter Write-Screen

show gui

This trick is also great for keeping track of all of the types of accounts you have to create. Most of the time, adding a new employee requires more than just an Active Directory account. Hide all of that complexity behind a controller function and hand the GUI application off to the junior administrator. Using abstraction to hide complexity.

create employee gui

Note: I’ve had better luck running Show-Command with PSReadline turned off. The issue has been raised as a PSReadLine GitHub issue.

Updated: Lots of people asked about how I created the GUI behind the New-Employee function GUI. I just used a defined parameter set and show command. I recreated it in a GitHub Gist. There isn’t too much you can customize beyond this but you also don’t have to edit any XAML!.

2 thoughts on “Poor Man’s GUI

  1. Do you have a code to share for the New-Empoyee?
    I am very interested as I use XAML and would like to compare what is easier to implement.

Leave a Reply

Your email address will not be published. Required fields are marked *