Get Out! (-GridView)

One of the more interesting cmdlets that was pointed out to me recently is Out-GridView. I found that for a script I wrote, being able to visually display choices and allowing those choices to be selected and acted on was an enormous benefit for certain functions. Specifically, when using the Out-GridView cmdlet with the -PassThru switch, we are enabling users to select objects from a list (prepopulated or gathered locally with variables) and pass those selections for a script. Let’s walk through a couple examples to help you visualize the benefits.

Example 1

Take an environment that has a lot of mailboxes, say over 100,000 and there are many name conflicts due to the same first name and last name combinations. However, by looking at other details we can differentiate the users. Then, by knowing who is who, we can then select those mailboxes and review their sizes. For this example we have three mailboxes and our differentiating factor is the Office Location. So when we query these mailboxes we pull the DisplayName, Alias, and Office Location:
[sourcecode language=”powershell”]
$Mailboxes = Get-Mailbox | Select DisplayName,Alias,Office
$Mailboxes | Out-GridView
[/sourcecode]
If we run the two lines above we can view a list of all mailboxes and then filter for mailboxes in the list:

However, we cannot do anything with this information. Selecting names does nothing and nothing is returned to the script. The question is then, how do we return selections? ‘-Passthru’:
[sourcecode language=”powershell”]
$Mailboxes | Out-GridView -Passthru
[/sourcecode]
Now we can select a mailbox:

By selecting a mailbox, we can see output returned to PowerShell:

We can also collect this information in a variable for later use like so:
[sourcecode language=”powershell”]
$SelectedMailboxes = $Mailboxes | Out-GridView -Passthru
[/sourcecode]
When this is run, we can verify the variable contents like so:

Example 2

Imagine you need to remove a list of files, either from a local on-premises file server or from a user’s one drive, being able to validate that the files you need to delete are correct would be useful. In the below example Out-GridView, a list of files was provided via CSV and now we are using PowerShell to first validate the files and display which ones that were found.

From there we can select which ones we want to remove:

Then, once the files are selected we can process each file and double-check that the file selected is to be deleted:

As we can see from the above examples, Out-GridView can provide a more user-friendly interface for those executing the scripts. Give it a try and comment if you find this tip useful. Thanks.

Related Post

CIM to WMICIM to WMI

Years ago WMI was de-emphasized for CIM. Wait. Do you know what those are? CIM – “The Common Information Model (CIM) is an extensible, object-oriented data model that contains information

Schedule It!Schedule It!

Schedule it. Automate it. Leveraging this function enables a coder to expand what they can accomplish with scripting in an environment. PowerShell scripts can do this with the Windows Scheduler