Category Archives: PowerShell

Friday Fun – Take a Chance with PowerShell

Last week I showed you my PowerShell Bingo game. While the game itself might be a fun way to pass the time, the real goal was to teach you some PowerShell techniques and concepts without you realizing it. This week, I thought I’d keep with the gaming theme and take up chance. Specifically let’s roll some dice and flip some coins. These aren’t especially complicated tasks in PowerShell, and it is hard to say what problem they might solve, but let’s have some fun anyway.

First up, flipping coins.

Flipping a coin is essentially a True/False game. Or another way to look at is Even/Odd. My favorite way is to use the modulo operator.

Those values can be represented as Booleans.

So all I need is a random number and perform a modulo operation. Here’s the function I created.

I used a valid verb but will also define a more user-friendly alias.

Because you may want several different types of output, not necessarily the best idea for a function but this might be an exception. And remember this is supposed to be educational not necessarily practical. My function uses parameter sets and depending on the set, decides what type of result to write to the pipeline.

And it seems to work pretty well.

Next, let’s roll some dice. Again I’ll use Get-Random . A single dice roll is as simple as this:

To roll multiple dice, you could do this:

Naturally I wrote a function with a few more bells and whistles because I like shiny toys.

This function lets you choose the number of dice to roll. Notice I use a ValidateRange attribute to limit the number of dice. And in case you are rolling for a game of Dungeons and Dragons which has some extra-sided dice, I gave you that option as well.

In some games, you need the total so I added that as well. One thing that I changed was my rolling technique. In this version I am pre-generating an array of all possible values and then get the specified number of random values.

Not that it is super critical, but this technique is faster.

Here’s the result

If you are debating between different techniques in a script or function, use Measure-Command to see how they perform.

Now you have some tools to build your own PowerShell games and maybe learn something new in the process. If you do, I hope you’ll share. Enjoy!

PowerShell Dates, Times and Formats

astroclock_thumbIf you are like me you use date time values constantly in PowerShell. From simply displaying the current date and time in progress message to using different values to create file or folder names. The Get-Date cmdlet has a -Format parameter which you can use. The tricky part is remembering what values to specify. Especially because they are case-sensitive. The values are documented at http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo%28VS.85%29.aspx.

So to make my life (and maybe yours) easier, I wrote a little PowerShell script to remind me of the possible values.

The variable $patterns is an array of commonly used datetime format values. Many of them were pulled from that MSDN page but I added some of my own at the end. Here’s the output from this script.
test-datetimepattern01

This looks nice, and is definitely a nice cheat sheet. But then I realized since I’m writing a script I should take this to the next level and write a useful object to the pipeline.

This version creates a custom object for each pattern, including the syntax.
test-datetimepattern02

An alternative to -Format is to use the ToStringMethod() specifying the format pattern.

Because this is an object I can pipe it to other cmdlets.

test-datetimepattern03

I included the syntax so all you need to do is copy and paste. Or, how about this?

Define this function in the PowerShell ISE, perhaps even adding it to your Add-Ons menu with a keyboard shortcut. When executed, you’ll get a graphical display using Out-Gridview.

test-datetimepattern04

Select a pattern and the syntax gets pasted into your PowerShell script. I love making PowerShell do my work for me! Enjoy.

Friday Fun – Let’s Play a Game

BingoCard-smallToday is going to be a lot of fun. A few years ago, back when we were still running PowerShell 2.0 everywhere, I created a module to run a Bingo game in a PowerShell session. I primarily wrote the module as a learning tool for beginners wanting to know more about how to construct a module. The module also includes examples of some techniques such as a custom format file, exporting (or not) members from a module and splatting.

Enough time has passed since I first worked on this that I decided to revisit and update for PowerShell 3.0 and later. I’ve gone through and cleaned up things that I had to do in v2 that now are better handled in v3 like ordered hash tables. I’ve also fixed a few bugs I missed the first time and added at least one new feature.

When you load the module, you can start a new bingo game by running Invoke-Bingo. I wanted to stick to an official verb for the function. But to make life easier, the module also defines aliases, Play-Bingo and simply, Bingo. Stick to standard verbs with your functions but feel free to add aliases to make your commands more user-friendly.

When you start a new game, I have a function that creates a custom object for the Bingo card. The old version launched a separate PowerShell window to call the numbers, but I have incorporated that into the main output.

bingo-1

You keep entering numbers as they are ‘called’ until a winning card is detected. There are separate functions for calling numbers and testing cards.
bingo-2

In this version I also added a parameter that will create a Speech object so that your computer can speak the called numbers to you! If you want to play multiple cards, open up separate PowerShell windows and run Invoke-Bingo -Cardonly.

bingo-3

This is also handy if there are several of you wanting to play. Only one person needs to be the caller. I should get together with Boe Prox and figure out a way to run this over a remoting session!

If you want to try this out, and I hope you do, download this zip file and extract to your modules folder. As always, I hope you pick up something useful. Or if nothing else you can kill some time waiting for that server reboot to finish.

Enjoy!