Friday Fun: Does Anyone Really Know What Time It Is?

eternalclock_150x150 In PowerShell it is brain-dead easy to get the date and time with Get-Date. If you look through articles I’ve posted you’ll find plenty of examples using Get-Date and the [DateTime] object. But now that we’re getting ready for a new year, I thought you might be planning ahead and might want a few shortcuts for datetime elements like month or day names.

There are no cmdlets for these things so you’ll have to rely on some .NET techniques, but don’t worry. You don’t have to give up being an IT Pro. The steps are very easy, even if it looks a bit foreign to you. First, let’s get the days of the week. These are nicely stored in a .NET class called DayOftheWeek which is an Enum. That’s just a fancy way of saying a “list”. Here’s how you can list the “list”.

Pretty cool. I don’t have a system running PowerShell in a different language but I’m assuming these values will reflect your culture. As far as I know this should work in any version of PowerShell. What can you do with this? Maybe you need a folder for each day of the week.

I took each value and added it to C:\Work to create the corresponding daily folder. Next, let’s figure out how to do with month names.

Unfortunately, there is no Enum class for month names. But, the information is part of a globalization class which corresponds to your computer culture. Here’s how it works for me.

Again, non-English systems should get months in your language. There is one slight bug with this. For some reason there is a mysterious 13th month with no name.

To get around this, simply filter for values.

We can use the same technique to create a set of monthly folders as well.

I piped each month name to ForEach-Object. In the begin scriptblock I created a top-level year folder and in the process scriptblock a folder for each month. Or perhaps you’d like to avoid hardcoded values like 2014 or include the year in the folder name.

Here I’m getting the current year in the Begin block and then appending it to the folder name. If I run this today I will end up with a folder like C:\Work\January_2013. Note that because I used an underscore, I had to escape it.

Finally, if you prefer abbreviated months you can do that as well.

Here too you have to filter out the mysterious null month.

So plan ahead for next year and I hope it is a great one for you.

2 thoughts on “Friday Fun: Does Anyone Really Know What Time It Is?”

  1. Very interesting and useful. For non-English version: I have Slovenian Windows 7 Powershell 3.0 version. I get months in Slovene – correctly, but days are in English. That is strange.
    PS C:\Skripti> get-culture

    LCID Name DisplayName
    —- —- ———–
    1060 sl-SI Slovenian (Slovenia)

    PS C:\Skripti> [enum]::GetValues([dayofweek])
    PS C:\Skripti> [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames

    1. That makes sense, although perhaps not useful. [DayOfWeek] is not localized. But notice that DateTimeInfo is part of the System.Globalization namespace which handles culture specific things like month names. But, try this and see if this doesn’t give you language specific day names.


Comments are closed.