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.
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.
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.
Here is how this example works:
- The $current variable contains the current date and time.
- The $tz variable contains the abbreviated timezone name.
- Then, we use the -join operator to concatenate these two variables together with a space in between them.
Suppose that our computer is in the Eastern Standard Timezone (EST).


The output displays the current date and time along with the timezone:
- 3/5/2024 8:26:22 AM EST

The output displays the current date and time along with the timezone:
- 3/5/2024 8:26:22 AM Eastern Standard Time
Feel free to use whichever method you prefer.
How to Compare Dates in PowerShell
How to Format a DateTime in PowerShell
How to Calculate Date Difference in PowerShell
Do you want to know how to format a date while working with PowerShell Get-Date? Here, I will show you how to use PowerShell Get-Date format to retrieve the current date and time, and format it in various ways. In this tutorial, we’ll explore how to use PowerShell Get-Date to format dates in different patterns, such as yyyymmdd, yyyymmddhhmmss, mm/dd/yyyy, and dd/mm/yyyy.
The yyyymmdd format is a common date representation that stands for a four-digit year, two-digit month, and two-digit day, all concatenated together without any separators.
In PowerShell, you can obtain the current date in yyyymmdd format using the Get-Date cmdlet with the -Format parameter:
Get-Date -Format "yyyyMMdd"# This will output the current date as something like 20240104
$currentDate = Get-Date -Format "yyyyMMdd"
Write-Output $currentDateHere, you can see the output in the screenshot below after I executed the above PowerShell script.

By default the Get-Date cmdlet in PowerShell returns the current date and time.
Method 1: Use (Get-Date).ToString()
Method 2: Use (Get-Date).Date
Both methods will return the current date without the time.
Example 1: Use (Get-Date).ToString()
One way to get the current date without the time in PowerShell is by using the ToString() method to simply format the current date and time using M/d/yyyy as the date format:

This returns 3/5/2024, which is the current date when this article is being written.
Note that the format M/d/yyyy specifies that only one digit should be used for the month and day if they month and day only contain a single digit.
If you would like to display two digits for the month and day, you can use the MM/dd/yyyy format instead:

This returns 03/05/2024.
Example 2: Use (Get-Date).Date
Another way to get the current date without the time in PowerShell is by using the .Date property of the Get-Date cmdlet.

This returns Tuesday, March 5, 2024 12:00:00 AM, which is the current date when this article is being written with the time component set to zero.
How to Compare Dates in PowerShell
How to Format a DateTime in PowerShell
How to Calculate Date Difference in PowerShell
I’m trying to get a script to test connectivity of a list of IP addresses and log the output to a file. I would like it to put a time/date stamp in the log when when I get no response. I’d like to execute the script over a 2-3 day period. I’ve got it to put the time/date stamp on the output of the console but not in the output file. Here’s the script.
$names=Get-Content "C:\script\hname.txt"
foreach($name in $names){ if(Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){ Write-Host "$name is up" -ForegroundColor Green $output+="$name is up," } else{ Write-Host "$name is down" -ForegroundColor Red Get-Date $output+="$name is down," " " }
}
$output | Out-File -FilePath "C:\script\results.txt"
Start-Sleep -s 3this is the results on the ps console
PS C:\Windows\system32> C:\Users\admin\Documents\Test Connection-3sec.ps1 192.168.1.254 is up 192.168.1.74 is down Tuesday, October 10, 2023 12:06:05 PM 192.168.1.239 is up 192.168.1.23 is down Tuesday, October 10, 2023 12:06:07 PM 192.168.1.189 is up 192.168.1.78 is up 192.168.1.79 is up 192.168.1.81 is up 192.168.1.89 is down Tuesday, October 10, 2023 12:06:08 PM 192.168.1.82 is up 192.168.1.85 is up 192.168.1.45 is down Tuesday, October 10, 2023 12:06:10 PM PS C:\Windows\system32>
But this is what I get in the results.txt file.
192.168.1.254 is up, 192.168.1.74 is down, 192.168.1.239 is up, 192.168.1.23 is down, 192.168.1.189 is up, 192.168.1.78 is up, 192.168.1.79 is up, 192.168.1.81 is up, 192.168.1.89 is down, 192.168.1.82 is up, 192.168.1.85 is up, 192.168.1.45 is down,
as a side note the format doesn’t have the new line feed which I understand I should be able to get if I add $output+="$name is up," +"`n`r"
I’ve tried putting the get-date at the end of the $output line, to no avail.
$output+="$name is down," Get-DateHow can I do this?
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-yyyyI’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 dddI’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:mmThe 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:ssI’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.fffI’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.
If you want to know how to get file modified date in PowerShell, check out this tutorial completely. In this blog post, we will explore various methods to retrieve file modification dates using PowerShell.
Get File Modified Date in PowerShell Using Get-Item Cmdlet
The Get-Item cmdlet in PowerShell is one of the simplest ways to retrieve the last modified date of a single file. It provides information about the file object, including its properties, such as LastWriteTime, which indicates the last time the file was written to.
Here is the complete PowerShell script.
$file = Get-Item "C:\MyFolder\MyFile.txt"
$lastModified = $file.LastWriteTime
$lastModifiedThis script assigns the file’s properties to the $file variable and then extracts the LastWriteTime property, storing it in the $lastModified variable.
You can see the screenshot below after I executed the script using VS code.

Get All Files Modified Date in PowerShell using Get-ChildItem Cmdlet
$files = Get-ChildItem "C:\MyFolder"
foreach ($file in $files) { $lastModified = $file.LastWriteTime Write-Host "$($file.Name) was last modified on $lastModified"
}This PowerShell script will iterate through each file in the specified directory, outputting the name of the file and its last modified date to the console.
Check out the screenshot below:

Format the Date Output
Sometimes, you might want to format the date output to a specific format. PowerShell makes this easy with the ToString() method, which can be applied to date objects.
$file = Get-Item "C:\MyFolder\file.txt"
$lastModified = $file.LastWriteTime.ToString("MM/dd/yyyy HH:mm:ss")Filter Files by Modification Date in PowerShell
If you’re looking for files modified within a certain date range, you can combine Get-ChildItem with the Where-Object cmdlet to filter the results.
Here is the PowerShell script that will provide list of files that have been modified in the last 30 days from the current date.
$dateCutoff = (Get-Date).AddDays(-30) # Files modified in the last 30 days
$files = Get-ChildItem "C:\path\to\directory" | Where-Object { $_.LastWriteTime -gt $dateCutoff }Get Folder Last Modified Date in PowerShell
If you want to include directories in your search and retrieve their last modified date, you can modify the Get-ChildItem cmdlet to include the -Directory parameter.
Here is the PowerShell script.
$directories = Get-ChildItem "C:\MyFolder" -Directory
foreach ($dir in $directories) { $lastModified = $dir.LastWriteTime Write-Host "$($dir.Name) was last modified on $lastModified"
}This script will output the name and last modified date of each directory within the specified path.
Conclusion
PowerShell provides multiple ways to retrieve file modification dates. In this tutorial, I have explained how to get file modified date in PowerShell. The easiest method is to use the Get-Item Cmdlet in PowerShell.
You may also like:
PowerShell Get-Date Format yyyymmddhhmmss
If you need more than just the date and require the time as well, the yyyymmddhhmmss format comes into play. This pattern includes the year, month, day, hour, minute, and second, providing a complete timestamp without separators.
To format the current date and time in yyyymmddhhmmss format, use PowerShell Get-Date with the -Format parameter:
Get-Date -Format "yyyyMMddHHmmss"# This will output the current date and time as something like 20240104153045
$currentDateTime = Get-Date -Format "yyyyMMddHHmmss"
Write-Output $currentDateTimeHere is the output like in the below screenshot.

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 %AI’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 %mI’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 %dPress 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 %RI’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.
PowerShell Format Date mm/dd/yyyy
The mm/dd/yyyy format is another common date representation, particularly in the United States, where the month precedes the day and year. To format a date in this style in PowerShell, you can use the Get-Date cmdlet with the -Format parameter, similar to the other formats we’ve discussed.
Here’s how you can get the current date in the mm/dd/yyyy format:
Get-Date -Format "MM/dd/yyyy"# This will output the current date as something like 01/04/2024
$currentDateUSFormat = Get-Date -Format "MM/dd/yyyy"
Write-Output $currentDateUSFormatIn this example, the MM represents the two-digit month, dd represents the two-digit day, and yyyy represents the four-digit year. The slashes / are used as separators to distinguish each part of the date clearly.
To ensure that you get the mm/dd/yyyy format regardless of the regional settings, you can use the below PowerShell script.
$currentDateUSFormat = Get-Date -UFormat "%m/%d/%Y"
Write-Output $currentDateUSFormatThe -UFormat parameter uses Unix format specifiers and should give you the date in the desired format of mm/dd/yyyy. For example, if today’s date is January 4, 2024, the output should be 01/04/2024.
Check out the screenshot below for the output:

PowerShell Format Date dd/mm/yyyy
Get-Date -Format "dd/MM/yyyy"# This will output the current date as something like 04/01/2024
$currentDateFormatted = Get-Date -Format "dd/MM/yyyy"
Write-Output $currentDateFormattedTo ensure that you get the dd/MM/yyyy format regardless of the regional settings, you can use the below PowerShell script.
$currentDateUSFormat = Get-Date -UFormat "%d/%M/%Y"
Write-Output $currentDateUSFormatYou can see the output like the below screenshot.

Conclusion
Formatting dates in PowerShell is straightforward with the Get-Date cmdlet. By using the -Format parameter with the desired pattern in PowerShell Get-Date, you can easily transform the current date and time into a string that fits your needs.
Here I have explained to you the below PowerShell get-date format examples.
- PowerShell get-date format yyyymmdd
- PowerShell get-date format yyyymmddhhmmss
- PowerShell format date dd/mm/yyyy
- PowerShell Format Date mm/dd/yyyy
You may also like:



