Contents

The Powerful PowerShell cmdlet Out-ConsoleGridView

Pass output to an interactive table straight in the console


In this post I will be sharing the PowerShell cmdlet Out-ConsoleGridView which is part of the ConsoleGuiTools module. Which is in my opinion well worth it to have in your toolbox!

Introducing The Powerful PowerShell cmdlet Out-ConsoleGridView

It has been a while since I have written a post. So long that I had to figure out how to get going again. Luckily I had my own blog post to go back to! Thank god for documentation :-)
I’m completely set again with Hugo as well as my sites theme updated to the newest versions. I was in the mood to “create something” so welp a blog post will have to do and nothing was going to stop me.

A while ago I stumbled upon the Out-ConsoleGridView PowerShell cmdlet and I have used it in my scripts ever since. I get to see the nice GUI look daily. Because I like it so much I thought that I might as well share it. Perhaps someone else may find it extremely useful as well!

Prerequisites


PowerShell 7

Only one prerequisite this time. Just good old PowerShell. PowerShell version 7 that is.

Installation

Before we dive in. Lets install the module that contains the cmdlet:

1
Install-Module Microsoft.PowerShell.ConsoleGuiTools

And we are good to go.

Out-ConsoleGridView

So what is this cmdlet all about? Well, it can send the output from one command to an interactive table, from which you can select items and pass them further down the pipeline.
What makes this so cool is that the GUI is within your console. Just use your arrow keys to go over the displayed items. Use the space bar to mark items and press enter when you are happy with your selection.
Or better yet.. Do you have a lot of output on the screen and can’t immediately find what you are looking for? Just type in a keyword in the filter box to search for the text in the table.
This is great if you have a command with some output where you want to search through with a convenient gridview.

Have a look for yourself:

1
Get-Process | Out-ConsoleGridView



The cmdlet is also useful in situations where you have to process output from one command and want to pass it to another command but still want to do some decision making in between.
For example I use the Azure PowerShell commands in my job a lot. This requires me to set my authentication to a certain subscription to which I want the cmdlets to run against in the current session.
Because both the subscription names as well as the subscription ids are not as easy to remember I first had to copy them from the Azure portal and then paste them into the terminal with the Set-AzContext cmdlet.
Or run Get-AzSubscription and then filter through the output and follow up with the copy-paste process.

/2022/04/powershell-out-consolegridview/out-consolegridview.webp

And if you want to follow along:

1
Get-AzSubscription | Out-ConsoleGridView -OutputMode Single | Set-AzContext

Isn’t this much nicer? I’m always amazed how much a simple oneliner in PowerShell can do!

For example

It atleast saved me from the copy-paste process! But I still had to deal with the difficult subscription names and ID’s.
If you add some nice descriptive tags on your subscriptions you can make use of those.
For instance a description (e.g. Kubernetes playground) and/or an environment (e.g. Non-Prod / QA / Prod) tag. An easy solution and a lot more user friendly!

This is the script that I wrote for this:

And of course there is a version for the Azure CLI as well.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
function Set-SubscriptionAzureCLI {
<#
.SYNOPSIS
    Wrapper function for the Azure CLI account commands to easily switch between subscriptions using consolegridview GUI.
#>
    #Requires -Modules Microsoft.PowerShell.ConsoleGuiTools
    #Requires -Version 7
    # AzureCli (+ resource-graph extension) | az extension add --name resource-graph
    $subscriptions = (az graph query -q "resourcecontainers | where type == 'microsoft.resources/subscriptions' | project name, subscriptionId, tags" | ConvertFrom-Json).data
    if ($LASTEXITCODE) {
        $_; EXIT 1
    }
    $subscriptionId = (($subscriptions | ForEach-Object {
                $description = $_.tags.description ? $_.tags.description : 'N.A.'
                $environment = $_.tags.Environment ? $_.tags.Environment : '---'
                [PSCustomObject]@{
                    name           = $_.Name
                    description    = $description
                    environment    = $environment
                    subscriptionId = $_.subscriptionId
                }
            }) | Sort-Object environment | Out-ConsoleGridView -OutputMode Single).subscriptionId
    $subscriptionId ? (az account set -s $subscriptionId) : "Exited. The current active subscription is:"
    az account show
}

However, in order to get a list of all subscriptions I am running a Kusto Query Language (KQL) query against the Azure Resource Graph. In order to do that you need to have the resource-graph extension installed for the Azure CLI, but that is a simple process:

1
az extension add --name resource-graph

Feel free to tweak both of the scripts as per your requirements. I use them many times per day and it has saved me tons of time.

Tip

Consider creating a PowerShell profile if you find yourself using custom functions regularly. The functions will be available in every session that you open from then on without having to run/import them. If you make any changes to your $PROFILE and want to make them available in your current session simply dot source the scriptfile by running . $PROFILE.

Or if you don’t like to swamp your profile, adding the function as part of a psm1 file to one of the module diretories:

1
$Env:PSModulePath -split ';'

Oh, and the Out-ConsoleGridView cmdlet works in the Azure Cloud Shell as well. Cool!

Conclusion

In this post I gave a small introduction to the Out-ConsoleGridView cmdlet and shared an example script.
I hope that you may find a use case for the cmdlet as well and can add it to your toolbox!

For further reading I suggest the documentation.

Have a very nice day! 👍