Tag Archives: Function

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!

Look at Me!

bluelight Last week I posted some ideas on how to add notifications to your scripts. Those ideas were variations on the old school “Press any key to continue” prompt that I assume many of you are familiar with. Most of those concepts should work for you, but they assume you looking at the PowerShell window. I thought about those situations where perhaps I only see a portion of the PowerShell window. Wouldn’t it be helpful if you had some other visual clue, like a flashing light? I thought so and whipped up Invoke-Flasher. I’ll admit the name might have an unexpected connotation, but you can always change it.

Here’s the function, and then I’ll explain how to use it.

This function will only work in the PowerShell console, not the PowerShell ISE because it uses the ReadKey() method from $host.ui.rawui to detect if the user hits any key. The main portion of the function keeps looping through until a key is pressed. Each time through the background color of the host UI is toggled between the current color and Red, or whatever console color you specify. Each time through the script writes your text and “Press any key to continue”. I use the Coordinates property of the host to write to the same spot on the screen each time so there’s no scrolling.

By default, the Write-Host line will “flash” by alternating the background color. Or you can use the -FullScreen parameter which will clear the host everytime. If you use this option in your script, make sure the main part of your script is saving data somewhere because you won’t see it. Here’s an example of how you might use it.

After the main portion of the script completes, the flashing message is displayed after the results. If you want to use the fullscreen approach, you could try something like this:

When the main portion of the script finishes you’ll get a flashing screen with the text message. Press any key and you’ll get the results.

I have to say I’m intrigued by this function and can already think of some ways to improve it. If you have suggestions or find this useful, I hope you’ll let me know.

Pimp your Prompt

bling2If you are like me and live in PowerShell, then you spend a great deal of your day looking at your PowerShell prompt. That little indicator in the console and ISE that usually shows where you are. That little part of your PowerShell world is defined by a built-in function called Prompt. You can easily see the function like this:

This prompt is from PowerShell v4 but I’m pretty sure it is the same function that was used in v3. PowerShell v2 has a different function.

Did you notice that the newer function has a help link? Try it:

help prompt -online

You’ll get the online version of the about_prompts help topic. The great thing about the prompt function is that you can change it. I’ve posted a variety of prompts over the years. But here are 4 more for you to try out. These prompts should work in v3 and later. Most of the functions are simple additions to the standard prompt and should work for both the console and ISE. To try out the prompt you can paste the function into your PowerShell session. To make it “permanent”, insert it into your PowerShell profile script.

Include PowerShell Version

This prompt inserts the PowerShell major version into your prompt.
version-prompt

Include Admin

This prompt will test if you are running as Admin and if so, it inserts [ADMIN] in red text.
adminprompt

Include Computername

Do you like how a remoting session shows you the computer you are connected to? Why not have that all the time? All I’ve done is insert the local computername from the Computername environmental variable.

computername-prompt

Auto Export Command History

This last version serves up a twist on transcription. When you run a transcript you get the command and results. But maybe all you want is a record of all the commands you ran. Sure, you could export command history at the end of your session, but you have to remember to do so and if you exceed your maximum history count, you’ll miss commands. In this prompt, everytime you hit enter, it gets the last command you ran and appends it to a log file. The log file is created in your PowerShell directory and uses the naming format of the PowerShell host, without spaces, a time stamp (YearMonthDay) and the process ID of the current PowerShell session. This allows you to keep multiple PowerShell sessions with separate logs. The log file will only record the command if it is different than the last one you ran. This also allows you to hit Enter without doing anything and not fill up your log.

If you temporarily paste in one of these Prompt functions, but don’t like it, you can simply restart PowerShell to get your original prompt. Or you can use this function to restore it.

This is handy to put into your PowerShell profile if you are experimenting with prompts. The Restore-Prompt simply defines a new Prompt function in the global scope. I’m using the default PowerShell prompt but you change it to whatever you wanted.

If you are doing something cool with your prompt, I hope you’ll share.