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.

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.

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.


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.


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.


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

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.


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.


Change is in the air

Petri_162x87If you have read my blog for any length of time, or followed me, then you know that I have contributed a lot of content to a number of online resources over the years, often on a recurring basis. You may not realize it, but spreading out my workload over several sites can be quite demanding. After a lot of deliberation, I’ve decided to consolidate all of my online writing efforts with the Petri IT Knowledgebase.

The Petri IT Knowledgebase has been around since 1999 and has always been a source of high quality content for IT Pros.  For the longest time it had a co.il domain name. But now you can find it at Petri.com. I will be publishing all of my online content exclusively at Petri where I will be a contributing editor. The site is being re-launched, lots of exciting things are in the works and I’m looking forward to being a part of it. At Petri, I will be primarily responsible for PowerShell related content. But you can expect to see me write about Active Directory, Group Policy, WMI, Hyper-V and Windows Server 2012 (and later).

I will still be blogging here and I will continue to create courses at Pluralsight. You will still find me at conferences like TechEd (or whatever it gets renamed) and the PowerShell Summit. I’ll still be doing PowerShell-related consulting and training on my own as well. Naturally, all of my existing content “in the wild” will remain where it is.

In most regards, nothing really changes other than the fact that you only have one place to check for my latest work. I hope you’ll come along for a great ride.

Advice, solutions, tips and more for the lonely Windows administrator with too much to do and not enough time.

%d bloggers like this: