In this tutorial, I will explain how to use the PowerShell Get-Date cmdlet to work with dates and times in your scripts. The Get-Date cmdlet allows you to retrieve the current date and time, format dates, and perform date calculations. As a system administrator or a developer, you should know how to use Get-Date in PowerShell effectively.
Retrieve the Current Date and Time using Get-Date Cmdlet
The most basic usage of the Get-Date PowerShell cmdlet is to retrieve the current date and time. By simply running the cmdlet without any parameters, it returns a DateTime object representing the current date and time:
Get-Date
This will output something like:
16 December 2024 13:45:33
Here is the exact output in the screenshot below:

Check out PowerShell Get-Date Minus 1 Day
Format Date and Time Output
While the default output format is informative, you may want to customize the way the date and time are displayed. The Get-Date cmdlet provides the -Format parameter, which allows you to specify a custom format string. Here are a few examples:
- Display the date in the “MM/dd/yyyy” format:
Get-Date -Format "MM/dd/yyyy"
Output: 12/16/2024
- Display the time in the “HH:mm:ss” format:
Get-Date -Format "HH:mm:ss"
Output: 10:30:45
- Display the date and time in a custom format:
Get-Date -Format "dddd, MMMM d, yyyy h:mm:ss tt"
Output: Wednesday, December 16, 2024 10:30:45 AM
You can refer to the Microsoft documentation for a complete list of available format specifiers.
Read PowerShell Get-Date Format
Working with Specific Dates
In addition to retrieving the current date and time, Get-Date allows you to work with specific dates. You can pass a date string to the cmdlet, and it will create a DateTime object representing that date. For example:
Get-Date -Date "2024-07-04"
This will output:
Thursday, July 4, 2024 12:00:00 AM
You can also specify a specific time along with the date:
Get-Date -Date "2024-07-04 13:30:00"
Read PowerShell Get-date Format Milliseconds
Date Calculations and Comparisons
PowerShell’s Get-Date cmdlet enables you to perform date calculations and comparisons. Here are a few common scenarios:
- Adding or subtracting days from a date:
$today = Get-Date
$futureDate = $today.AddDays(30)
$pastDate = $today.AddDays(-7)
- Comparing dates:
$date1 = Get-Date -Date "2024-12-01"
$date2 = Get-Date -Date "2024-12-16"
if ($date1 -lt $date2) {
Write-Host "Date 1 is earlier than Date 2"
}
- Calculating the difference between two dates:
$startDate = Get-Date -Date "2024-01-01"
$endDate = Get-Date -Date "2024-12-31"
$difference = $endDate - $startDate
Write-Host "Days between the dates: $($difference.Days)"
Read How to Get-Date Without Time in PowerShell?
PowerShell Get-Date Examples
Now, let me show you some real examples of Get-Date PowerShell cmdlet.
- Generate a file name with the current date:
$fileName = "Backup_" + (Get-Date -Format "yyyyMMdd") + ".zip"
This will create a file name like Backup_20241216.zip, which can be helpful for organizing daily backups.
- Check if a certificate is expiring soon:
$cert = Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object { $_.Subject -eq "CN=www.example.com" }
$expirationDate = $cert.NotAfter
$daysRemaining = ($expirationDate - (Get-Date)).Days
if ($daysRemaining -lt 30) {
Write-Host "The certificate is expiring in less than 30 days!"
}
This script retrieves a certificate from the Windows certificate store, calculates the number of days until its expiration, and displays a warning if it’s expiring soon.
You can also check a video tutorial on the same.
Conclusion
In PowerShell, you can use the Get-Date cmdlet to work with dates and times in your scripts. Whether you need to retrieve the current date, format dates, perform date calculations, or compare dates, you can use the Get-Date cmdlet. I hope you now understand how to work with the PowerShell Get-Date cmdlet with these useful examples.
You may also like:
- PowerShell Get-Date To String
- How to Convert String to DateTime in PowerShell?
- How to Get the Last Modified Date of a File in PowerShell?
- PowerShell Get-date Add Days Examples

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.
Thank you so much for this knowledge Artical.