How to Use PowerShell Get-Date Cmdlet?

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:

PowerShell Get-Date Cmdlet

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:

  1. Display the date in the “MM/dd/yyyy” format:
   Get-Date -Format "MM/dd/yyyy"

Output: 12/16/2024

  1. Display the time in the “HH:mm:ss” format:
   Get-Date -Format "HH:mm:ss"

Output: 10:30:45

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

  1. Adding or subtracting days from a date:
   $today = Get-Date
   $futureDate = $today.AddDays(30)
   $pastDate = $today.AddDays(-7)
  1. 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"
   }
  1. 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.

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

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

1 thought on “How to Use PowerShell Get-Date Cmdlet?”

Leave a Comment

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial

FREE Power Platform Tutorial PDF

Download 135+ Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…