Category Archives: PowerShell

Friday Fun Reverse PowerShell

reverseThese Friday Fun posts are not supposed to be practical, yet hopefully still entertaining and educational. Today’s article is no exception but I ‘ll be you’ll try it at least once. And you’ll maybe even learn something about PowerShell that perhaps you didn’t know before. It all starts with a string. We use strings all the time for things like passwords and names. Let me start by explaining some basic concepts and then we’ll get to the fun stuff.

The string object has a number of methods, but I’m not going to use any of them today. Instead, I want to start by showing that the string is an array, or collection, of characters. This means you can access any element by its index number.

You can start at the end of the array with -1. You can also get several elements with the Range ( .. ) operator.

So, what about counting in reverse? We know we can start at -1. -2 would give us the 2nd to last element, -3 the third to last and so on. Therefore all we need to do is count backwards to the beginning.

I used the string length to know how far back to count. But wait, it gets better. Let’s bring in the -Join operator. This operator is designed to join elements of an array into a single string.

If you don’t want to specify a delimiter, you can use -Join like this:

See where I’m going with this? Let’s join the reversed array of string characters.

So if I can take any string and reverse it, why not reverse PowerShell? I created a function called Out-Reverse.

The idea is that you can run any PowerShell expression, pipe it to Out-Reverse, and you’ll get just that: reverse output.
out-reverse

The function takes any pipeline input and stores it in a temporary array. Once everything has been processed, the data is converted to a string with Out-String. Because I need to process each line separately, I split what would otherwise be a very long single string, into multiple strings on the end of line marker (`n).

$Text is now an array of strings and I can reverse each one.

It is kind of fun looking at reversed output. For you language geeks, this also makes a great palindrome tester.

Or maybe you need some simple obfuscation.

Actually, if you find a production-worthy use of this function I hope you’ll share it with me and the community. But as always I hope you picked up a PowerShell tidbit along the way that you can use in your “real” PowerShell work.

Enjoy your weekend. I’m sure you earned it.

Friday Fun: A Random PowerShell Console

crayonsThis week I thought we’d have a little fun with the PowerShell console and maybe pick up a few scripting techniques along the way. Today I have a function that changes the foreground and background colors of your PowerShell console to random values. But because you might want to go back to your original settings without completing restarting PowerShell the function allows you to reset to your original values. Oh, and it also supports -Whatif. Here’s what I came up with.

The function will not work properly in the PowerShell ISE so I’ve included some code at the beginning to see if it is running in the ISE. If so, the command displays a warning and bails out. This is a scenario where using Return is completely acceptable as I want to return out of the pipeline without doing anything. I could have used a command like this: Return “This command only works in the PowerShell console.” but that would have written a string object to the pipeline and I don’t want anything to go to the pipeline. Plus, I prefer to use Write-Warning for messages like this.

When you run the command, it tests for the existence of some variables, $savedfg and $savedbg, that are defined in the global scope. You’ll notice the use of the global: prefix. If they are not defined, then the assumption is that this is the first time the command has been run and the variables will be defined with the current values of the $host.ui.rawui.foregroundcolor and $host.ui.rawui.backgroundcolor values. Later, if you use -Reset, the command will use these values to restore your original settings.

Otherwise, the command gets a random color for the System.ConsoleColor enumeration for the background and then another random color for the foreground, that is different than the randomly selected background color.

When you look at the code, you will see that I am specifying in the cmdletbinding attribute that the function supports ShouldProcess. That could also be written [cmdletbinding(SupportsShouldProcess=$True)] but that seems redundant to me. Anyway, the command to make the change, $host.ui.rawui.backgroundcolor= $bg, by itself doesn’t know anything about ShouldProcess and -WhatIf. But I can add my own code using an If statement.

The value for the ShouldProcess() method is the text you see as part of the “…performing operation on target…” message. The text is the target.
set-randomconsol-whatif

All I’ve done is define a variable, $msg, to make the line of code easier to read. As you can see, it is not that difficult to add your own support for -WhatIf. And now, if you get a little bored, mix it up for a fresh perspective. I’ve even included an alias, src, in case you have to type in a console where you can’t see what you’re typing.

Have a terrific weekend.

So you need to write a PowerShell script

lightbulb-idea So…you have decided to write a PowerShell script or have at least identified a need. What do you do first? If you say “Google or Bing”, I’d say you are wrong. In my opinion, when you are developing a PowerShell script, searching for an existing script is not the first step. Sure, you will likely find something, but….

There are many online sources of PowerShell scripts and code samples. However, from my experience the quality is all over the board. Sure, you might find a great example. But unless you have a great deal of PowerShell experience, how will you judge? Sadly, many script and code samples I see don’t follow community accepted best practices, don’t follow the PowerShell paradigm or are simply bad scripts. There is also absolutely no guarantee that the script or code sample you download will work correctly (and safely) in your environment.

Personally, I would recommend that you open your script editor and start laying out a series of comments about what it is that you need to accomplish. If you are using the ISE you might even use regions to outline your script. This task helps your organize your work, and when you are finished, the script is documented? All you have to do is write the code to fulfill the comments. Yes, that might be difficult and maybe even a little time consuming at first, but that’s the point. You will be learning much more than by simply copy and pasting something of dubious quality you found online. Even more importantly, you will be developing something that you know will work in your environment.

Get stuck? Then sure, look online to find examples of how someone used a particular cmdlet or function. But try to find several examples and “average” them out. Perhaps even better would be to post in the forums at PowerShell.org and ask for specific help on a sticky problem. You’ll likely get several responses. And knowing the quality of the average PowerShell.org forum member, I’d feel very comfortable with their responses.

Over time, as you gain more PowerShell experience, you will be better able to assess the quality of online PowerShell scripts and samples. I still think you should develop on your own from scratch, but I also think you’ll find the process goes much faster.