Tag Archives: Function

A Better PowerShell More

In PowerShell, when I have a lot of output, I can use the legacy more.com command to page the results to the screen.

There’s not anything inherently wrong with this approach. Although one drawback is that it doesn’t work in the PowerShell ISE. For that reason alone I decided I needed a PowerShell version of More.  I wanted a command that would take pipelined input and write the output in “pages” of objects.  You can think of the page as a group of objects. The premise is simple enough: take the incoming objects, and pass them on to the pipeline when the number of objects meets the page limit.

My function, Out-More, is on GitHub.

The default page count is 50. I set a maximum value of 1000 which is totally arbitrary. I wanted to emulate the More.com command the best I could so when you are prompted you can hit Enter or press M to get the next page or N to get the next object.  You can also Quit. One thing my command can do that More.com cannot is to stop paging and simply display the rest of the objects.  This means you can page output for a few screens and then display the remaining objects.

Here’s an example:

More PowerShell Output
Using Out-More

To be clear here, this is writing output to the pipeline not to the console.  My expectation is that  Out-More would be the last command in your expression,  but you could save the results to a variable using Tee-Object.

out-more to variable
Out-More to a variable

The script file will also define an alias of om.  I debated using More, but that is actually a built-in PowerShell function that wraps More.com so I decided to leave it alone. A few of the command parameters also have aliases so take a few minutes to read the help.

I hope you find this as useful as I do.

Friday Fun: Size Me Up

Part of day job involves creating training material, often in the form of video training for Pluralsight or articles for Petri.com. Since I usually am covering PowerShell I often need to capture a PowerShell session. And sometimes I want the screen to be a particular size. So over time I’ve created a few PowerShell tools to resize console and application windows. The PowerShell console window stores its dimension under $host.ui.rawui.windowsize.

These are the same settings you would see here:

As long as you use a value less than the buffer dimensions, you can modify the console window from a prompt. But it takes a step you might not realize. You can’t do this:

Instead, you can create a new type of object with your intended dimensions.

Then you can use this object as a value for the WindowSize property.

Naturally, I created a function to do this for me.

My function also includes code to support –WhatIf.

Of course now that I’ve shown you that I have an alternative. You can use the .NET class [System.Console] which has properties for width and height. And you can set these values independently.

You can’t discover this unless you know something of the .NET Framework, but you could have discovered $host which is why I showed you that first. Since I often need to record video at 1280×720 dimensions, I wrote a quick and dirty script to set my PowerShell console window to those dimensions.

Everything I’ve shown you so far is for the PowerShell console. But what about the ISE? You can’t use the techniques I’ve covered. Application windows are bit more complicated and I’m not going to go into the details. But I came across some code on GitHub (https://gist.github.com/coldnebo/1148334). I don’t do Minecraft but it didn’t take much to turn it into a re-usable function.

The code supports –WhatIf and defaults to the current application, which is presumably the PowerShell ISE.

But this is what actually gets set.

So if you wanted to include the title bar you would need to adjust accordingly.

All of this may not really be applicable to your work, but if you find a good use I hope you’ll let me know. Have a great weekend.

Baby, It’s Cold Outside

I don’t know about your neck of the woods, but it is downright Arctic here. So I thought I’d polish up my PowerShell function to get weather data.

This function uses the Yahoo weather RSS feed for US locations. The function uses the Invoke-RestMethod to get an entry for a given zip code. I set my zip code as the default and suggest you do the same. You will need to modify the default value in the code above. Invoke-RestMethod gives me an XML document so it isn’t too difficult to pull out the values I want and construct a custom object. I even use some regular expression named captures to break out the location and time. So hopefully there are some good learning examples here.

Anyway, quite frigid here, and this is already 5 degrees warmer than when I got up.

weather

Hope you are staying warm.