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