Tag Archives: Out-Gridview

What PowerShell Script Was I Working On?

Last week I shared a script for finding recently modified files in a given directory. In fact, it wouldn’t be that difficult to find the last files I was working on and open them in the PowerShell ISE. Assuming my Get-RecentFile function is loaded it is a simple as this:

But often, I already have the PowerShell ISE open. True, I could use the same expression substituting ‘psedit’ for ‘ise’, but with the ISE I’m all about working quickly So I have another function targeted for the PowerShell ISE called Open-LastScript.

The core commands in this function are very similar to my other script. And I probably could get by with one script so consider Open-LastScript as a variation on a theme. The function gets PowerShell related files from my Scripts directory. I could have hard coded a default value, but instead I set it to a global variable called ScriptPath which I define in my PowerShell ISE profile script. If that variable isn’t defined, or if nothing is specified for –Path, then the function uses the current location, assuming it is a FileSystem path.

But the fun part, at least for me, is that I take the results of the DIR command and create a few custom properties, including one that shows the number of lines in the script file.

The selected objects and properties are then piped to Out-Gridview. Starting in PowerShell 3.0 you can pass objects back to the pipeline from Out-Gridview. In other words, you can use Out-Gridview as a simple object picker. The function by default gets the 10 most recently modified scripts.

I can select as many entries as I want and click OK. Each selected script will then be opened in the ISE. Yes, I know I can see similar information in the most recently used list but that can show files outside of my Scripts folder and I’m limited to maximum MRU count. My function lets me specify the number of files to list.

The bottom line is that my Open-LastScript function is a handy tool that saves me a little bit of time finding the files I need to work on and every little bit helps. I could create an Add-Ons menu shortcut to run the function with the defaults, but for some reason I opted for an alias.

The alias gives me a little bit of flexibility.

I hope this gives you some ideas of how you can use PowerShell to make your scripting more efficient, or at the very least as an example of PowerShell scripting techniques.

Browsing PowerShell Commands

Whenever I’m exploring a new PowerShell module or snapin, one of the first things I do is list all of the commands found within the module.

You can specify either a module or a snapin. Use the -module parameter for both. However, for larger modules, I’ve realized I need a better way to browse the commands. For example, I might need to see them organized by verb or noun. That information is included with the Get-Command expression. I simply have to ask for it. Here’s a more thorough command and the result.

get-command-mod

I included the command type because some modules might contains cmdlets and functions. I could revise this expression and insert a sort command. But that’s too much typing. Especially if I want to sort and re-sort. Instead, I’ll pipe this command to Out-Gridview.

get-command-mod-ogv

Now I have a sortable and filterable view of all the commands. Plus, it is in a separate window so I have my prompt back and get help for listed commands. I even decided to build a quick-and-dirty function.

Because the module or snapin name can include a wildcard, I added the module name to the output. Now I have a tool to grab all the commands from a module or set of modules and I can filter and browse all I want without having to retype or revise commands at the prompt.

Friday Fun: View Objects in a PowerShell GridList

One of the things that makes PowerShell easy to learn is discoverability. Want to know more about a particular type of object? Pipe it to Get-Member. Or if you want to see values pipe it to Select-Object.

That’s not too bad. Or you can pipe to Out-Gridview.

However, for a single object it isn’t easy to see everything .
Select-OutGridView

What would be easier, at least for me, is to use Out-Gridview but see the properties in a list like I can with Select-Object. So I wrote an advanced function to do just that. The function takes an incoming object, creates a hashtable based on the object’s properties and then displays the hashtable in Out-Gridview.

The function will accept multiple objects, which is why I have a data array to handle multiple piped in objects. But it really works best with a single object. In the Process scriptblock I pass the object to Get-Member to retrieve properties. Then the function creates a hashtable where the key is each property and the value is the corresponding value from the object.

At this point I could simple write the hashtable to Out-Gridview, but the column headings would say Name and Value. It’s a small detail but I use a hashtable with Select-Object to rename “Name” to “Property”. By the way, you may have noticed this line.

That isn’t a typo. If I pipe multiple objects, then $data will be an array of hashtables and so I need to get an enumerator for each one. But with this function now I get the display I want.

OutGridList

There really isn’t any filtering to do but you can click on the column headings to sort. Or your can run Out-Gridlist with -passthru so you can select properties and pass them through to the pipeline.

I hope you find this useful. Enjoy responsibly.

Friday Fun A Graphical PowerShell History Picker

One of my favorite features in PowerShell 3.0 is that you can select items in Out-Gridview which will then pipe the object back to the pipeline. One way I’ve been using this is as graphical “picker” for command history. I use Get-History, actually its alias h, all the time. Once I know the history number I then use Invoke-History, or its alias r. Now, with Out-Gridview, which has an alias of ogv, I can sneak in a little something extra.

I realize this is cryptic but when using the shell interactively efficiency is paramount. This is the actual full command.

I’ll get something like this:

history-picker

I can select an item, click OK and the command will run back in my console. There are a few downsides, but remember this is a Friday Fun article. First, if you cancel, PowerShell will invoke the last command again. Also, as written if you select multiple items, which you could with -Passthru, you’ll get an error because Invoke-History won’t accept multiple entries. You could force Out-Gridview to only allow a single selection.

Or you could use Foreach to handle multiple selections.

Be aware that when you run multiple commands in the same pipeline formatting can get a little screwy so I would probably stay way from it.

One more way you might use this is with Invoke-Expression (iex) instead of Invoke-History. The history object has a commandline property which you could invoke.

This will also handle multiple commands. If you cancel from Out-Gridview, Invoke-Expression will throw an exception but at least it won’t try to re-run the last command which is probably better.

Again, these are all “quick and dirty” commands to have fun with from a PowerShell prompt.

PowerShell Graphing with Out-Gridview

I’ve received a lot of interest for my last few posts on graphing with the PowerShell console. But I decided I could add one more feature. Technically it might have made more sense to turn this into a separate function, but I decided to simply modify the last version of Out-ConsoleGraph. The new version adds a switch parameter called -Gridview. When used, the graphing information is sent to the Out-Gridview cmdlet. This version will require PowerShell 3.0 as I’ll explain.

I realized I could just as easily send the values for the object name, property and graphing figure to Out-Gridview. In the ForEach construct where each object is processed, if the -GridView parameter is specified, I’ll create an ordered hash table and add it to an array.


...
if ($GridView) {
#add each object to the gridview data array
$gvHash = [ordered]@{
$CaptionProperty = $caption
$Property = ($g*$graph)
Value = $obj.$Property
}
$gvData += New-Object -TypeName PSObject -Property $gvHash
}
...

I need an ordered hash table so that the property values remain in the same order. I could have piped the new object to Select-Object and specified the order, but hopefully many of you are moving to v3 anyway. There’s no need for me to pad the caption since the grid view lines things up in columns automatically. It is also easier to add a property with the actual property value. I wanted to incorporate that value into the console version, but it proved to be more complicated than expected. Using Out-Gridview is a nice alternative.

After all of the objects have been processed, if there is anything in $gvData, I send it Out-Gridview.


...
if ($gvData) {
Write-Verbose "Sending data to Out-Gridview"
$gvData | Out-GridView -Title $Title
}
...

When using the grid, there is no way to specify color. But on the other hand, it is much easier to click on a column heading and re-sort. Here’s a command I ran with the new function.


Get-ChildItem C:\Scripts -Directory | foreach {
$data = Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Property Length -sum
$_ | Select Name,@{Name="Size";Expression={$data.sum}}
} | Out-ConsoleGraph -Property Size -Title "Scripts Folder Report" -grid

out-consolegraph-gv

Now I have a tool with some flexibility. Although you might also be interested in this article from last November on building a drive report graph also using Out-Gridview.

I bumped the latest version of Out-Consolegraph to 3.0 to align it with the file name. Download Out-ConsoleGraph-v3