. Advertisement .
..3..
. Advertisement .
..4..
The Get-Date cmdlet can help you get date and time using PowerShell. This guide will help you get the hang of it and some other related techniques.
Get Date And Time Using PowerShell
Get-Date
You can use the Get-Date cmdlet to create a character string of a time or date and send it to another program or cmdlet. Thanks to the cross-platform nature of PowerShell, this cmdlet can produce date and time in various formats on .NET and UNIX systems.
The simplest use is to call it without any options like this:
PS C:\> Get-Date
Wednesday, August 10, 2022 3:50:38 PM
In the above example, the cmdlet displays your system’s current date and time in the long-time and long-date formats.
There are plenty of ways to use Get-Date, but they all generate and use DateTime objects to represent the date and time you specify. You can check this with the GetType()
method.
PS C:\> (Get-Date).GetType().FullName
System.DateTime
DateTime Objects Formatting
By default, it uses your computer’s settings to determine the format of the output. You can use (Get-Culture).DateTimeFormat to view these settings:
PS C:\> (Get-Culture).DateTimeFormat
AMDesignator : AM
Calendar : System.Globalization.GregorianCalendar
DateSeparator : /
FirstDayOfWeek : Sunday
CalendarWeekRule : FirstDay
FullDateTimePattern : dddd, MMMM d, yyyy h:mm:ss tt
LongDatePattern : dddd, MMMM d, yyyy
LongTimePattern : h:mm:ss tt
MonthDayPattern : MMMM d
PMDesignator : PM
You can change the way Get-Date displays date and time with the -Format option. It works like custom date and time formats in the .NET framework.
Example:
PS C:\> Get-Date -Format "dd-MM-yyyy HH:mm:ss"
10-08-2022 18:57:36
The -UFormat parameter and its format specifiers are also available:
PS C:\> Get-Date -UFormat "%A %m/%d/%Y %R %Z"
Wednesday 08/10/2022 19:18 +07
Keep in mind that when it is used, the cmdlet only retrieves necessary properties from the DateTime object to complete the format. Therefore, some methods and properties might not be available.
Methods Of DateTime Objects
The DateTime objects in PowerShell share many similar methods with the class of the same name in .NET. In particular, there are four common methods you can use to convert their values to equivalent string representations:
ToLongDateString()
: return the long date representation of a DateTime objectToShortDateString()
: return the short date representation of a DateTime objectToLongTimeString()
: return the long time representation of a DateTime objectToShortTimeString()
: return the representation of a DateTime object
These examples show how those methods work in PowerShell:
PS C:\> (Get-Date).ToLongDateString()
Wednesday, August 10, 2022
PS C:\> (Get-Date).ToShortDateString()
8/10/2022
PS C:\> (Get-Date).ToLongTimeString()
7:15:06 PM
PS C:\> (Get-Date).ToShortTimeString()
7:15 PM
Convert Strings To DateTime Objects
PowerShell provides the ability to convert from strings to DateTime objects:
PS C:\> $date = "11-03-2020 11:05"
PS C:\> $dateobj = [DateTime]$date
You can check the type of these objects with GetType()
:
PS C:\> $date.GetType().Name
String
PS C:\> $dateobj.GetType().Name
DateTime
Arithmetic Operations With Dates And Times
If you want to add hours, days, etc., to your current date and time, you just need to create a DateTime object with the Get-Date cmdlet first, then invoke the AddHours, AddDays, etc. methods.
PS C:\> (Get-Date).AddHours(3)
Wednesday, August 10, 2022 7:39:40 PM
PS C:\> (Get-Date).AddDays(7)
Wednesday, August 17, 2022 4:39:58 PM
Compare Date And Times
You can use the comparison operators in PowerShell to compare different DateTime objects created by the Get-Date cmdlet:
PS C:\> $date1 = (Get-Date).AddDays(-20)
PS C:\> $date2 = (Get-Date).AddDays(1)
PS C:\> $date2 -gt $date1
True
PS C:\> $date1 -lt $date2
True
PS C:\> $date3 = "01/01/2023"
PS C:\> $date4 = "January 1, 2023"
PS C:\> [DateTime]$date4 -eq [DateTime]$date3
True
In those comparisons, the -eq, -gt, and -lt operators let you check whether the first object’s value equals, is greater than, or is less than that of the second DateTime object.
Summary
You can get date and time using PowerShell with the Get-Date cmdlet. It is a feature-rich function with plenty of options for customizing formats as well as arithmetic operations. Want to put those commands into a script? This guide would be a great help.
Leave a comment