Creating a Simplistic GUI Interface with Out-GridView

A couple of weeks ago I noticed a post in the forums at PowerShell.org where a question was asked about selecting a group of servers in Server Manager and running a PowerShell script against that selection of machines. While my solution doesn’t accomplish exactly what I think they were looking for, it does the next best thing.

First, I added a menu item to Server Manager called MrToolkit (short for Mike Robbins Toolkit):

gridview-gui1.png

The item I added is a .bat file that contains the following code:

powershell.exe -ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -Command "Invoke-Command -ComputerName (Get-ADComputer -Filter {OperatingSystem -like 'Windows Server*' -and enabled -eq $true} | Out-GridView -OutputMode Multiple -Title 'Select Servers to Query:' | Select-Object -ExpandProperty DNSHostName) -FilePath (Get-ChildItem -Path C:\Scripts\*.ps1 | Out-GridView -OutputMode Single -Title 'Select PowerShell Script to Run:' | Select-Object -ExpandProperty FullName) | Out-GridView -Title 'Results' -PassThru"

Notice the tweaks in the previous code for bypassing the execution policy and running it with no profile. I also had to add the PassThru parameter to the last Out-GridView otherwise the command finished and exited without the user being able to see the results.

Those tweaks aren’t necessary if you just want to run the actual PowerShell code:

Invoke-Command -ComputerName (
    Get-ADComputer -Filter {OperatingSystem -like 'Windows Server*' -and enabled -eq $true} |
    Out-GridView -OutputMode Multiple -Title 'Select Servers to Query:' |
    Select-Object -ExpandProperty DNSHostName
) -FilePath (
    Get-ChildItem -Path C:\Scripts\*.ps1 |
    Out-GridView -OutputMode Single -Title 'Select PowerShell Script to Run:' |
    Select-Object -ExpandProperty FullName
) | Out-GridView -Title 'Results'

One thing to note is using the Out-GridView cmdlet requires the PowerShell ISE to be installed and Out-GridView being able to send its results to the pipeline was a feature introduced in PowerShell version 3 so those are the minimum requirements.

This command first returns a list of all the servers in the domain that are enabled:

gridview-gui2.png

After selecting the Servers you want to run a script against and clicking <OK> as shown in the previous example, the user is prompted to select one of the scripts that exists in the specified directory:

gridview-gui3.png

The user is limited to selecting one script, after selecting it and clicking <OK>, the selected script is run on the remote machines and the results are displayed:

gridview-gui4.png

When the user clicks <OK>, the Out-GridView window and all traces of the command disappear. How’s that for a simplistic GUI interface for your PowerShell scripts?

ยต