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.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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