Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

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

Posted on December 27, 2013

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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".

PS C:\> [enum]::GetValues([dayofweek])
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

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.

[enum]::GetValues([dayofweek]) | foreach {
  mkdir "c:\work\$_"
}

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.

PS C:\> [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames

January
February
March
April
May
June
July
August
September
October
November
December

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.

PS C:\> [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames | measure

Count    : 13
Average  : 
Sum      : 
Maximum  : 
Minimum  : 
Property : 

To get around this, simply filter for values.

[System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames | where {$_} 

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

[System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames |
where { $_ } | foreach -begin { mkdir C:\Work\2014 } -process {
  mkdir "c:\work\2014\$_"
}

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.

[System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames |
where { $_ } |
foreach -begin {$yr = (Get-Date).year} -process { mkdir "c:\work\$_`_$yr" }

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.

[System.Globalization.DateTimeFormatInfo]::CurrentInfo.AbbreviatedMonthNames | 
where { $_ } |
foreach -begin {$yr = (Get-Date).year} -process { mkdir "c:\work\$_`_$yr" }

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

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

  1. Roman Mazi says:
    December 27, 2013 at 3:57 pm

    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])
    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    PS C:\Skripti> [System.Globalization.DateTimeFormatInfo]::CurrentInfo.MonthNames
    januar
    februar
    marec
    april
    maj
    junij
    julij
    avgust
    september
    oktober
    november
    december

    1. Jeffery Hicks says:
      December 27, 2013 at 4:06 pm

      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.

      [System.Globalization.DateTimeFormatInfo]::CurrentInfo.DayNames

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d