PowerShell Problem Solver: Create Numbered Output Lists with PowerShell

Posted on April 15, 2015 by Jeff Hicks in PowerShell with 0 Comments

I came across an interesting question the other day in a PowerShell forum. The poster wanted to include numbers for each line of output, which might look like this:

I wasn’t sure at first why you would want to do this, as this feels like text parsing and not taking advantage of PowerShell’s object nature. But I started playing along. Here’s one approach.

Which gives you this:

A numbered output list created with Windows PowerShell. (Image Credit: Jeff Hicks)

A numbered output list created with Windows PowerShell. (Image Credit: Jeff Hicks)

That’s not bad I suppose if all you want to do is look at the list or save it to a text file because all you have is text. Although, you could use it to build an interactive console menu.

I’m saving the results to $menu, so that I can access them later in the example.

An interactive console in Windows PowerShell. (Image Credit: Jeff Hicks)

An interactive console in Windows PowerShell. (Image Credit: Jeff Hicks)

That’s sorta cool. But because this is PowerShell, we should simply add a property.

I am referencing the variable in the global scope because otherwise $i would be new for each object and everything would have a value of 1 in the Expression scriptblock. If I were to pipe this to Get-Member, I would see a new property called Item#. Given that, here’s my revised menu code:

Our revised output. (Image Credit: Jeff Hicks)

Our revised output. (Image Credit: Jeff Hicks)

This is all fun and proof of concept stuff. There’s no error handling, and if you run it again without setting $global:i back to 0, then your numbering will be off.

Sponsored

Everything I’ve shown you thus far is an ad-hoc approach, although certainly it lends itself to scripting. Another option would be to add your own type and formatting extensions. Let’s look at the formatting options. To create a custom format, you’ll need to create a .ps1xml. One way to get started is to export the current settings. Since I’m working with service objects, I can use an expression like this:

You can also open up $pshome\DotNetTypes.format.ps1xml in the ISE or copy and paste the relevant section into a new file. Now, you can edit and create your own formatting file. Here’s what I came up with:

I changed the Name setting because I will use this to reference this view. What I’m creating is in addition to the normal formatting. Next, I’ve created a new heading for the item number.

Naturally I need to provide a value.

There is a concession here you have to accept. There is no easy way in the XML file to initialize $global:I every time you format the results.

Sponsored

The first time everything will look fine, but unless you reset $i it will keep increasing every time you format the output with this custom view. Once you have it complete, you can load it into your session and use it.

The get-service cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

The get-service cmdlet in Windows PowerShell. (Image Credit: Jeff Hicks)

Because this is formatted output, it is pretty to look at or save to a text file, but that’s the limits of its usefulness. If you want the Item number to be a part of the object, then you’ll need a custom type extension. This is actually pretty easy since we are adding a single property.

However, there is the same caveat about $global:i.

Although if you were using this in a script or function you could handle initializing the variable and cleaning up at the end.

Finally, you could take all of this one more step and create your own custom object with its own type and formatting directives, but I’ll leave that fun exercise to you.

Sponsored

Tagged with ,