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.
