Share this post: This is part of an ongoing blog series by Adam Gordon. Adam will show you how to use each PowerShell command each week. Adam will be covering Get-Date this week.
When to use Get Date
The Get-Date cmdlet returns a DateTime object representing the current date or one you specify.
Get-Date formats the date and time in a variety of.NET and UNIX formats.
Get-Date can be used to generate a date/time character string and then send it to other cmdlets, or programs.
Get-Date uses your computer’s culture settings as a guide to format the output. To view your computer’s settings, use (Get-Culture).DateTimeFormat
How to use Get-Date
Find the current date and hour:
Get-Date displays current system time and date. The output can be found in both long-date and long-time formats.
Find elements of the current date/time:
Get-Date -DisplayHint Datum
Get-Date can be used to retrieve the date or the time element. The -DisplayHint parameter accepts the arguments Date, Time or DateTime.
Use a.NET format specifier to get the date and time
Get-Date -Format “dddd MM/dd/yyyy HH:mm K”
Get-Date uses a -Format parameter in order to specify multiple.NET format specifiers. The output is a String object.
These are the.NET format specifiers that were used in this example:
SpecifierDefinitionddddDay of the week – full nameMMMonth numberddDay of the month – 2 digitsyyyyYear in 4-digit formatHH:mmTime in 24-hour format -no secondsKTime zone offset from Universal Time Coordinate (UTC)
Click here to find out more about the.NET format specifiers.
Use a UFormat specifier to get the date and time
Get-Date -UFormat “%A %m/%d/%Y %R %Z”
Get-Date uses a -UFormat parameter for specifying several format specifiers. The output is a String object.
These are the UFormat format specifiers that were used in this example:
SpecifierDefinition%ADay of the week – full name%mMonth number%dDay of the month – 2 digits%YYear in 4-digit format%RTime in 24-hour format -no seconds%ZTime zone offset from Universal Time Coordinate (UTC)
Find a date’s year-end:
(Get-Date -Year 2020 -Month 12 -Day 31).DayOfYear
Get-Date uses three parameters for the date: Year (Month), and Day (Day). The command is enclosed in parentheses to allow the DayofYear property to evaluate it.
NOTE: The Gregorian Calendar has 365 days, with the exception of leap years which have 366. December 31, 2020 is day 366.
You can check if a date has been adjusted for daylight saving time:
$DST = Get-Date$DST.IsDaylightSavingTime()
This example uses a simple boolean method that checks if a date has been adjusted by daylight saving time. Variable $DST stores the Get-Date result.
$DST uses IsDaylightSavingTime to determine if the date has been adjusted for daylight saving time.
Convert the current time into UTC time
Get-Date -UFormat “%A %B/%d/%Y %T %Z”$Time = Get-Date$Time.ToUniversalTime()
In this example, the current hour is converted into UTC time. To convert the time, the UTC offset is used for the system’s location.
Get-Date uses a -UFormat parameter and format specifiers to display current system date/time. The UTC offset for -07 is represented by the format specifier %Z.
The $Time variable stores current system date and times. $Time uses ToUniversalTime() to convert 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
This example shows how a format specifier creates an object called timestamp String for a directory name. The timestamp contains the date, time and UTC offset.
The $timestamp variable stores results from a Get-Date operation. Format is used by Get-Date to create a timestamp string object.
The object is sent to ForEachObject via the pipeline. A ScriptBlock is the $_ variable that represents current pipeline object. The timestamp string can be delimited using colons, which are then replaced by periods.
New-Item uses Path to specify the location of a new directory. The directory name is specified by the $timestamp variable in the path. The -Type parameter indicates that a directory has been created.
Do you need PowerShell training? ITProTV offers PowerShell online IT training courses.