Jul 14

Time to get Visual

Using PowerShell gives you a simple text based console to run your commands from, but sometimes you want to jazz it up a bit or make it easier for non-techie folks to run your Cmdlets.  So how do we make it purtty?

PowerShell allows the use of Windows Forms for displaying and entering information, but what do I need to make it soft and GUI?  I hear you ask.  As it happens not a lot…

  $form = New-Object Windows.Forms.Form
  $draw = $form.ShowDialog()

And that’s it. No really it is…

Well ok maybe I did mislead a little, but this code snippet will start you on your way to creating GUI application using PowerShell.

Forming your output

Ok so I maybe jumped the gun a bit, there are ways to simply put out your list information into a GUI format.  We should all know by now, and if you don’t read my earlier posts, that you can display a list of your users with this cmdlet:

  Get-MsolUser

This will in turn produce this output:

Get-MsolUser

But with the simple addition of a pipe in your command line and an output cmdlet:

  Get-MsolUser|Out-GridView

The output changes to look like this:

Get-MsolUser-GUIMuch purrtier 😀

Using a windows GUI brings it’s own rewards, you also get the additional ability of being able to set filters to shorten your list if you have a large tenancy.

Doing something worthwhile with it all

So all’s well and good but how do I put this together to make something usable.

This script will set up a form that give you three buttons which will allow you, once connected, to query your Users, Groups and Registered Domains all in a purtty windows style;

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object Windows.Forms.Form
$form.Size = New-Object Drawing.Size @(400,200)
$form.StartPosition = "CenterScreen"
$form.Text = "PowerShell-O365 GUI"

function LoadForm()
{
    $layout = New-Object "System.Windows.Forms.FlowLayoutPanel"
    $layout.FlowDirection = "TopDown"
    $layout.Dock = "Fill"
    $layout.Autosize = "True"

    $panel = New-Object "System.Windows.Forms.GroupBox"
    $panel.Text = "Show"
    $panel.AutoSize = "True"
    
    $flow = New-Object "System.Windows.Forms.FlowLayoutPanel"
    $flow.Dock = "Fill"
    $flow.AutoSize = "True"

    $btn = New-Object System.Windows.Forms.Button
    $btn.add_click({Connect-MsolService})
    $btn.Text = "Connect"
    $layout.Controls.Add($btn)

    $btn = New-Object System.Windows.Forms.Button
    $btn.add_click({Get-MsolUser|Out-GridView -Title "Users"})
    $btn.Text = "Users"

    $flow.Controls.Add($btn)

    $btn = New-Object System.Windows.Forms.Button
    $btn.add_click({Get-MsolGroup|Out-GridView -Title "Groups"})
    $btn.Text = "Groups"

    $flow.Controls.Add($btn)

    $btn = New-Object System.Windows.Forms.Button
    $btn.add_click({Get-MsolDomain|Out-GridView -Title "Registered Domains"})
    $btn.Text = "Domains"

    $flow.Controls.Add($btn)

    $panel.Controls.Add($flow)
    $layout.Controls.Add($panel)
    $form.Controls.Add($layout)

}


LoadForm

$drc = $form.ShowDialog()

So this is a good start I would say;

PowerShell O365 GUI

There is nothing stopping you using the same simple functionality to allow you to create forms to add edit and update, users, and group or any other functionality that can be implemented on a command line.

The full script can be access on GitHub as PowerShellO365GUI.ps1.