PowerShell Datetime Tutorials

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-Date

This command will output something like:

Friday, September 13, 2024 9:15:30 AM

Format 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)
$customDate

This 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)
$previousDate

Compare 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)
$date

PowerShell Datetime Tutorials

Below is a list of more advanced and useful PowerShell Datetime tutorials.

TutorialsOne-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 DayExplains 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 UTCExplains retrieving the current date and time in UTC rather than local time.
PowerShell Get-Date Format ISO 8601Demonstrates formatting DateTime to ISO 8601 compliant strings.
How to Get Yesterday’s Date in PowerShellShows several ways to return “yesterday” as a DateTime value.
PowerShell Date ComparisonProvides 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 WeekExplains 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 MonthReturns 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 PowerShellCovers 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 MinutesFocuses on finding the difference between two dates in minutes.
PowerShell Get-Date Month NameShows how to retrieve the month’s name from a DateTime object.
PowerShell Get-Date -UFormat ExamplesProvides 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 SecondsDemonstrates converting time spans into a total number of seconds.
Convert Seconds to Hours and Minutes in PowerShellLearn how to convert seconds to hours and minutes in PowerShell.
How to Convert Seconds to Minutes in PowerShellCheck out the tutorial to know how to convert seconds to minutes in PowerShell.
100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.