This is a part of an on-going blog series written by Adam Gordon. Each week, Adam will walk you through a PowerShell command, showing you when and how to use each one. This week, Adam covers Get-Date.
When to use Get-Date
The Get-Date cmdlet gets a DateTime object that represents the current date or a date that you specify.
Get-Date can format the date and time in several .NET and UNIX formats.
You can use Get-Date to generate a date or time character string, and then send the string to other cmdlets or programs.
Get-Date uses the computer’s culture settings to determine how the output is formatted. To view your computer’s settings, use (Get-Culture).DateTimeFormat
How to use Get-Date
Get the current date and time:
Get-Date
Get-Date displays the current system date and time. The output is in the long date and long-time formats.
Get elements of the current date and time:
Get-Date -DisplayHint Date
Use Get-Date to get either the date or time element. The -DisplayHint parameter uses the arguments Date, Time, or DateTime.
Get the date and time with a .NET format specifier:
Get-Date -Format “dddd MM/dd/yyyy HH:mm K”
Get-Date uses the –Format parameter to specify several .NET format specifiers. The output is a String object.
The .NET format specifiers used in this example are defined as follows:
Specifier | Definition |
dddd | Day of the week – full name |
MM | Month number |
dd | Day of the month – 2 digits |
yyyy | Year in 4-digit format |
HH:mm | Time in 24-hour format -no seconds |
K | Time zone offset from Universal Time Coordinate (UTC) |
Click here for more information about .NET format specifiers.
Get the date and time with a UFormat specifier:
Get-Date -UFormat “%A %m/%d/%Y %R %Z”
Get-Date uses the –UFormat parameter to specify several format specifiers. The output is a String object.
The UFormat format specifiers used in this example are defined as follows:
Specifier | Definition |
%A | Day of the week – full name |
%m | Month number |
%d | Day of the month – 2 digits |
%Y | Year in 4-digit format |
%R | Time in 24-hour format -no seconds |
%Z | Time zone offset from Universal Time Coordinate (UTC) |
Get a date’s day of the year:
(Get-Date -Year 2020 -Month 12 -Day 31).DayOfYear
Get-Date uses three parameters to specify the date: Year, Month, and Day. The command is wrapped with parentheses so that the result is evaluated by the DayofYear property.
NOTE: The Gregorian calendar has 365 days, except for leap years that have 366 days. December 31, 2020 is day 366.
Check if a date is adjusted for daylight savings time:
$DST = Get-Date
$DST.IsDaylightSavingTime()
This example uses a boolean method to verify if a date is adjusted by daylight savings time. A variable, $DST stores the result of Get-Date.
$DST uses the IsDaylightSavingTime method to test if the date is adjusted for daylight savings time.
Convert the current time to UTC time:
Get-Date -UFormat “%A %B/%d/%Y %T %Z”
$Time = Get-Date
$Time.ToUniversalTime()
In this example, the current time is converted to UTC time. The UTC offset for the system’s locale is used to convert the time.
Get-Date uses the –UFormat parameter with format specifiers to display the current system date and time. The format specifier %Z represents the UTC offset of -07.
The $Time variable stores the current system date and time. $Time uses the ToUniversalTime() method to convert the time based on the computer’s UTC offset.
Create a timestamp:
$timestamp = Get-Date -Format o | ForEach-Object { $_ -replace “:”, “.” }
New-Item -Path C:\Test\$timestamp -Type Directory
In this example, a format specifier creates a timestamp String object for a directory name. The timestamp includes the date, time, and UTC offset.
The $timestamp variable stores the results of a Get-Date command. Get-Date uses the Format parameter with the format specifier of lowercase o to create a timestamp String object.
The object is sent down the pipeline to ForEach-Object. A ScriptBlock contains the $_ variable that represents the current pipeline object. The timestamp string is delimited by colons that are replaced by periods.
New-Item uses the Path parameter to specify the location for a new directory. The path includes the $timestamp variable as the directory name. The –Type parameter specifies that a directory is created.
Need PowerShell training? Check out ITProTV’s PowerShell online IT training courses.