Browse for and display a PowerShell Clixml file in a DataGrid window

I’ve been using Out-DataGrid a lot recently, I seem to have a lot of uses for it. One of the things I’m displaying is an array of custom objects. Sometimes the script that generates this data takes a while to run, in which case it’s handy to dump the array into an xml file via the Export-Clixml cmdlet.

You can then read this back in and display it in a DataGrid via one line:

Import-Clixml -Path <filename> | Out-DataGrid

This also has the benefit that you can share the data with people who don’t have the necessary PowerShell modules or permissions to run the script that generated the data.

To make this even easier I’ve written a short script to let you browse for the file and then display it. You can just run the script from an Explorer window or shortcut, no typing necessary!

Function Get-OpenFileName($initialDirectory){   
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    $SaveFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $SaveFileDialog.initialDirectory = $initialDirectory
    $SaveFileDialog.filter = "XML files (*.XML)| *.XML"
    $SaveFileDialog.ShowDialog() | Out-Null
    $SaveFileDialog.filename
}

$XMLPath = Get-OpenFileName -initialDirectory ([environment]::GetFolderPath("mydocuments"))

if($XMLPath -ne ""){
    $XML = Import-Clixml -Path $XMLPath
    $XML | Out-GridView -Wait
}

The if statement is there in case you click “cancel” or close on the browse dialogue. The -Wait stops the DataGrid closing immediately if you run the script from Explorer.

This entry was posted in PowerShell, Scripting and tagged , , , , , . Bookmark the permalink.

One Response to Browse for and display a PowerShell Clixml file in a DataGrid window

  1. Pingback: Find vSphere VM locations: Folder, vApp and Resource Pool | Robin CM's IT Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s