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.
- How to Add Days to a Date in PowerShell?
- PowerShell Get-Date Minus 1 Day
- How to Get Date and Time in 24-Hour Format in PowerShell?
- How to Get Date Differences Between Two Dates in PowerShell?
- How to Check If Date Is Older Than 30 Days in PowerShell?
- How to Compare Dates Without Time in PowerShell?
- PowerShell Get-Date UTC
- PowerShell Get-Date Format ISO 8601
- How to Get Yesterday’s Date in PowerShell
- PowerShell Date Comparison
- How to Add Months to Date in PowerShell?
- PowerShell Get Day of Week
- How to Get the Last Business Day of the Month Using PowerShell?
- How to Get Day of Month in PowerShell?
- How to Get the First Day of Next Month in PowerShell?
- PowerShell: Get the Last Day of the Previous Month
- How to Get the Last Friday of the Month Using PowerShell?
- Concatenate String and DateTime in PowerShell
- PowerShell (Get-Date).AddDays(0)
- PowerShell Get Difference Between Two Dates in Minutes
- PowerShell Get-Date Month Name
- PowerShell Get-Date -UFormat Examples
- How to Find Dates in Strings with PowerShell?