Category Archives: Scripting

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!

The Altaro PowerShell Hyper-V Cookbook

powershell-cookbook-ebook1 For awhile now I’ve been contributing to the Altaro Hyper-V blog. Many of my articles centered on managing Hyper-V with PowerShell. I’ve gone through my library of management tools and written an eBook around them. All of the PowerShell scripts and functions have been polished up a bit. The eBook briefly discusses how to you use each recipe. It is a cookbook after all.

All of the recipes have been tested, but even so you should test everything in a non-production environment. Every Hyper-V environment has its own quirks. Or you can use the recipes as jumping-off points for your own PowerShell and Hyper-V. When you download the eBook be sure to get the accompanying zip file of code samples.

Visit http://www.altaro.com/hyper-v/new-ebook-altaro-powershell-hyper-v-cookbook/ to learn more and download your free copy. I hope you’ll let me know what you think