If you want to become an expert in PowerShell, then you should know how to work with PowerShell datetime objects. In this page, you will get all the helpful tutorials related to PowerShell Datetime.
What is PowerShell DateTime?
PowerShell DateTime allows you to work with dates and times in your scripts. Whether you need to log events, schedule tasks, or manipulate date values, you will use DateTime in PowerShell.
Basic Syntax
To get the current date and time in PowerShell, you can use the Get-Date cmdlet. This cmdlet retrieves the current date and time based on your system’s settings.
Get-DateThis command will output something like:
Friday, September 13, 2024 9:15:30 AMFormat DateTime in PowerShell
Often, you’ll need to format the date and time to match specific requirements. PowerShell offers several ways to format DateTime, including the -Format parameter and the ToString() method.
Using the -Format Parameter
You can format the date directly using the -Format parameter with Get-Date. For example, if you want to display only the year, month, and day:
Get-Date -Format "yyyy-MM-dd"Using the ToString() Method
Another way to format DateTime in PowerShell is by using the ToString() method. This method is particularly useful when working with variables in PowerShell.
$date = Get-Date
$date.ToString("yyyy-MM-dd HH:mm:ss")Check out How to Format Date in PowerShell
Create Custom DateTime Objects in PowerShell
Sometimes, you might need to create a custom DateTime object rather than using the current date and time in PowerShell. You can do this by instantiating a new DateTime object.
$customDate = [datetime]::new(2024, 9, 13, 14, 30, 0)
$customDateThis command creates a DateTime object for September 13, 2024, at 2:30 PM.
Add and Subtract Time in PowerShell
PowerShell makes it easy to perform arithmetic operations on DateTime objects. You can add or subtract days, hours, minutes, and seconds using the Add methods.
Add Time
Here is a script to add time.
$currentDate = Get-Date
$newDate = $currentDate.AddDays(5)
$newDate
Subtract Time
Here is the PowerShell script to subtract time.
$currentDate = Get-Date
$previousDate = $currentDate.AddDays(-5)
$previousDateCompare Dates in PowerShell
Comparing dates is another common task you will face as a PowerShell developer. You can compare two DateTime objects using standard comparison operators.
$date1 = Get-Date
$date2 = [datetime]::new(2024, 9, 13)
if ($date1 -gt $date2) {
"Date1 is later than Date2"
} else {
"Date1 is earlier than or equal to Date2"
}Check out Get Date Without Time in PowerShell
Convert Strings to DateTime in PowerShell
Sometimes, you might have date and time values as strings that you need to convert to DateTime objects. You can use the ParseExact method for this purpose.
$dateString = "2024-09-13 14:30:00"
$dateFormat = "yyyy-MM-dd HH:mm:ss"
$date = [datetime]::ParseExact($dateString, $dateFormat, $null)
$datePowerShell Datetime Tutorials
Below is a list of more advanced and useful PowerShell Datetime tutorials.
| Tutorials | One-line description |
|---|---|
| How to Add Days to a Date in PowerShell? | Shows how to use AddDays() to increase or decrease a date value. |
| PowerShell Get-Date Minus 1 Day | Explains subtracting one day from the current DateTime using Get-Date. |
| How to Get Date and Time in 24-Hour Format in PowerShell? | Demonstrates formatting DateTime output in 24‑hour clock style. |
| How to Get Date Differences Between Two Dates in PowerShell? | Covers calculating the time span between two DateTime values. |
| How to Check If Date Is Older Than 30 Days in PowerShell? | Teaches how to compare dates to find items older than a set number of days. |
| How to Compare Dates Without Time in PowerShell? | Shows comparing dates while ignoring the time portion of DateTime. |
| PowerShell Get-Date UTC | Explains retrieving the current date and time in UTC rather than local time. |
| PowerShell Get-Date Format ISO 8601 | Demonstrates formatting DateTime to ISO 8601 compliant strings. |
| How to Get Yesterday’s Date in PowerShell | Shows several ways to return “yesterday” as a DateTime value. |
| PowerShell Date Comparison | Provides methods and operators for comparing DateTime values. |
| How to Add Months to Date in PowerShell? | Uses AddMonths() to shift a DateTime forward or backward by months. |
| PowerShell Get Day of Week | Explains extracting the weekday name or number from a DateTime. |
| How to Get the Last Business Day of the Month Using PowerShell? | Calculates the last weekday of a month, skipping weekends. |
| How to Get Day of Month in PowerShell? | Shows how to get the day number (1–31) from a DateTime value. |
| How to Get the First Day of Next Month in PowerShell? | Demonstrates deriving the first calendar day of the upcoming month. |
| PowerShell: Get the Last Day of the Previous Month | Returns the final date of the month before the current one. |
| How to Get the Last Friday of the Month Using PowerShell? | Shows logic to compute the last Friday for any given month. |
| Concatenate String and DateTime in PowerShell | Covers joining formatted DateTime values with regular text strings. |
| PowerShell (Get-Date).AddDays(0) | Explains why and how AddDays(0) is used in DateTime expressions. |
| PowerShell Get Difference Between Two Dates in Minutes | Focuses on finding the difference between two dates in minutes. |
| PowerShell Get-Date Month Name | Shows how to retrieve the month’s name from a DateTime object. |
| PowerShell Get-Date -UFormat Examples | Provides examples of using -UFormat for Unix-style DateTime formatting. |
| How to Find Dates in Strings with PowerShell? | Explains parsing and extracting date values from text strings. |
| PowerShell Convert Time to Seconds | Demonstrates converting time spans into a total number of seconds. |
| Convert Seconds to Hours and Minutes in PowerShell | Learn how to convert seconds to hours and minutes in PowerShell. |
| How to Convert Seconds to Minutes in PowerShell | Check out the tutorial to know how to convert seconds to minutes in PowerShell. |