Tag Archives: Function

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.

Using Optimized Text Files in PowerShell

documentIf you are like many IT Pros that I know, you often rely on text files in your PowerShell work. How many times have you used a text file of computernames with Get-Content and then piped to other PowerShell commands only to have errors. Text files are convenient, but often messy. Your text file might have blank lines. Your list of computernames might have a extra spaces after each name. These types of issues will break your PowerShell pipeline.

You can use Where-Object and filter out these types of problems. Or you can use a new function I wrote called Optimize-Text.

I’ve tried to add features to this command for what I think are common issues with text files. The core features are to remove any blank lines and trim leading and trailing spaces from each line of text. I’m working under the assumption that you will be using a command like Get-Content with some sort of list that you want to use with another cmdlet. You can insert Optimize-Text, in between.

For example, perhaps you want a quick and dirty ping test for a list of computers:

In this example, computers.txt is pretty mangled. There are blank lines and some names have leading and/or trailing spaces. Using Optimize-Text, fixes those issues. But wait…there’s more!

The advantage to using PowerShell is that everything is an object. Often, it helps to take advantage of pipeline binding. For example, the Computername parameter for Test-Connection accepts pipeline input by property name. So if the incoming object has a property that matches the parameter name, it will use it. Optimize-Text allows you to specify a property name. When you do, each line is turned into a custom object with a single property name.

This means I can run a command like:

And if you download this function today, I’ll throw in parameters to convert each line of text to upper case, to ignore lines with a specified comment character and to do additional filtering with a regular expression pattern!

By the way here’s what the text file looks like:

While it would be nice to think that all of the text files you use in PowerShell and neat and tidy, there’s no guarantee someone else might not come along and mess it up again. Hopefully, this function will help.

Please let me know how this works for you in the real world, or what other common problems you run into with text files. Be sure to look at full help and examples. Enjoy!