In this video tutorial, PowerShell expert Brien Posey explains how to use the Get-Date cmdlet in PowerShell to output date and time in various formats. He covers two parameters, -Format and -UFormat, and demonstrates how to customize the output by using different formatting characters.
The examples include displaying the day of the week, date, time, and milliseconds, highlighting PowerShell’s flexibility for creating log files with timestamps in a format that suits your specific requirements.
The transcript has been lightly edited for length and clarity.
Brien Posey: Recently, I've been hard at work on a custom PowerShell script that I'm going to use to automate various tasks and my environment. Now, because of the nature of the script and some of the things that it does, I need to design the script so that it produces a log file. That way, I have a way of going back and seeing exactly what the script did and when.
Now, as you can imagine, it's going to be important for this log file to contain a date and timestamp for each log entry. And that's what I wanted to show you how to do because PowerShell makes it easy to output the date and time.
It's done by using the Get-Date cmdlet. When I press Enter, you can see that today is Thursday, November 30, 2023, and the time is 4:42:19 p.m.
The interesting thing about the Get-Date cmdlet is that you can format the date and time in any way that you want. You can display just the date or just the time. You can put it in the format that makes the most sense for your own application.
So, I will show you a few of the different options that are available to you.
So, what I'm going to do is repeat the Get-Date cmdlet and, this time, append the -Format parameter:
There are any number of different formats and characters that PowerShell provides that you can use to format the date and time as you see fit. It's worth noting that these formatting characters are case-sensitive. Sometimes you'll get a completely different result or maybe even an error if you end up using the wrong case.
I'm not going to go through all the formatting characters, but I do want to show you some of the more useful ones.
Starting with something simple: I'll type MM-dd. You'll notice that the M's are uppercase and the D’s are lowercase. I'll press Enter. And you can see the date expressed as 11-30.
What if we wanted to attach a year to this? Well, that's easy enough to do. I'll repeat the command and then I'll type -yyyy:
Get-Date -Format MM-dd-yyyy
I'll press Enter, and now we see the date as 11-30-2023.
What if we wanted to put spaces between this instead of dashes? Well, we could do that, as well. But anytime that you include a space in the formatting string, you must enclose the entire string in quotation marks. So, I'll repeat the command and put a quotation mark in front of the month, then replace the dashes with a space, and then I'll put a closing quotation mark.
“Get-Date -Format MM dd yyyy”
I’ll press Enter, and now you can see the date expressed as 11 30 2023 without the dashes.
What else can we do? Well, another thing that you can do is you can get PowerShell to show you the day of the week. I'm going to type Get-Date and then -Format. To display the day of the week, I simply type ddd:
Get-Date -Format ddd
I'll press Enter, and that gives me the day of the week. You'll notice that this is abbreviated. I see “Thur” as opposed to “Thursday.” I'll show you how to display the full word a little bit later.
Another thing that you can do with the Get-Date cdmlet is to display the time. If we wanted to see the current time, all we'd have to do is type:
Get-Date -Format HH:mm
The H's are uppercase, and the M's are lowercase. When I press Enter, I see the time. The time is in military format, so 16:45.
If we wanted to append seconds to the time, that's easy enough to do. We simply repeat the command and then type :ss.
Get-Date -Format HH:mm:ss
I'll press Enter. Now we get the time with seconds. So, 16:45:50.
We could even go so far as to append milliseconds. If I wanted to do that, I would repeat the command and then at the very end type .fff.
Get-Date -Format HH:mm:ss.fff
I'll press Enter, and now you can see milliseconds have been added to the timestamp.
But what if we wanted to bring all this together and display both the date and the time? Well, it's easy to do that. All we must do is repeat what we did, but this time I'm going to create a longer string and enclose this in quotation marks. I'm going to type:
Get-Date -Format “ddd MM-dd-yyyy HH:mm”
I press Enter, and we see Thursday, 11/30, 2023, at 16:47.
So, that's just a quick sample of some of the things that you can do with the format parameter.
Alternate Formatting With -UFormat Parameter
The interesting thing about the Get-Date cmdlet is that the -Format parameter isn't the only parameter that you can use to format the output. There is another parameter called -UFormat. And I have no idea why Microsoft created two separate formatting parameters, but they did. Now, the thing that makes -UFormat different from -Format is that the formatting string is entered in a completely different way. With -Format, you saw that we use standalone characters – like HH, MM, dd, ddd, and things like that. When we use -UFormat, all the formatting parameters have to have a percentage sign in front of them. Let me show you an example.
Get-Date -UFormat %A
I'll press Enter, and when I do that, you can see the day of the week, in this case, Thursday. You'll recall that earlier, we saw an abbreviation for Thursday. It was “Thur,” as opposed to the entire word being spelled out. Here we have the entire word.
What else can we do? Well, another thing that we can do is to display the month. So, I'll type:
Get-Date -UFormat %m
I'll press Enter. And we can see that the month is 11.
Now, as I mentioned, the Get-Date cmdlet is case-sensitive. If you start swapping uppercase letters for lowercase letters, and vice versa, you can end up with some strange things. For example, if I were to replace %m with %M, I get 49. I have no idea what that 49 corresponds to.
Let's look at some other things that we can do. Another thing that we can do is to use a lowercase “d.”
Get-Date -UFormat %d
Press Enter, and we get the day of the month, in this case, 30. Today is November 30, hence the number 30.
What if we substituted the lowercase d for an uppercase D? Well, in this case, we get the entire date string, but without any formatting applied: 113023. We get the date displayed in six digits.
There are some other things that we can do. For example, we could do %R:
Get-Date -UFormat %R
I'll press Enter, and when I do that, we see the time in military time 1650.
If I change %R to %r, I see the time expressed as 04:50:12 PM.
Now, one of the things that we did with the -Format parameter was enclose the formatting string in quotation marks. Then, we were able to format the output in any way that we wanted. We can do the same thing with -UFormat.
So, what I'm going to do is repeat the same command, but now add quotation marks. Let's use a few different formatting elements.
Get-Date -UFormat “%A %D %r”
I’ll press Enter, and we can see Thursday 113023 at 04:51:05 PM.
This is just one example of how you can format the output. There are any number of different formats and characters that you can use to format the output exactly as you want it to be.
So, that's just a small taste of how you can use PowerShell’s Get-Date cmdlet to display the date/time.
I'm Brien Posey. Thanks for watching.
About the authorBrien Posey is a bestselling technology author, speaker, and 21x Microsoft MVP. In addition to his ongoing work in IT, Posey has trained as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.em>