Finding the Current Date with PowerShell
One of the easiest ways to discover the current date with PowerShell is by using the Get-Date cmdlet. This command displays the current date and time as shown below.
PS> Get-Date
Sunday, November 22, 2020 1:25:57 AM
By default, PowerShell Get Date command looks like it only returns the current date and time but, in reality, it’s actually returning a lot more information. To find this information pipe the output to the Format-List cmdlet as shown below.
PS> Get-Date | Format-List
DisplayHint : DateTime
Date : 11/22/2020 12:00:00 AM
Day : 22
DayOfWeek : Sunday
DayOfYear : 327
Hour : 1
Kind : Local
Millisecond : 163
Minute : 26
Month : 11
Second : 2
Ticks : 637416051621635773
TimeOfDay : 01:26:02.1635773
Year : 2020
DateTime : Sunday, November 22, 2020 1:26:02 AM
You can also use the Get-Member cmdlet to find all of the object properties by running Get-Date | Get-Member -MemberType Properties also.
If you check the type of object Get-Date returns, you’ll notice it’s a System.DateTime object type. This class is what exposes all of the different properties and methods you see. You can discover it’s object type by using Get-Member or using the GetType() as shown below.
PS> (Get-Date).GetType().FullName
System.DateTime
Once you see all of the available properties you have available, you can then reference them with dot notation as shown below.
PS> (Get-Date).Year
2020
PS> (Get-Date).DayOfWeek
Sunday
PS> (Get-Date).Month
11
PS> (Get-Date).DayOfYear
327
Is dot notation a bit too “devvy” for you? If so, you can also use the DisplayHint parameter to achieve the same result.
PS> Get-Date -DisplayHint Date
Sunday, November 22, 2020
PS> Get-Date -DisplayHint Time
2:29:35 AM
PS> Get-Date -DisplayHint DateTime
Sunday, November 22, 2020 2:29:39 AM
Using PowerShell Get Date is the simplest way to find the current date and various attributes but you can do so much more!
Date Arithmetic with PowerShell
Let’s say that you need to know the date and/or time a certain number of days, years, or minutes in the past or in the future. You can find this information using methods.
Using DateTime Methods
The System.DateTime object that PowerShell Get Date command returns has various methods you can invoke to add or remove chunks of time. If you run Get-Date | Get-Member, you’ll see various methods that start with Add.
AddDays Method datetime AddDays(double value)
AddHours Method datetime AddHours(double value)
AddMilliseconds Method datetime AddMilliseconds(double value)
AddMinutes Method datetime AddMinutes(double value)
AddMonths Method datetime AddMonths(int months)
AddSeconds Method datetime AddSeconds(double value)
AddTicks Method datetime AddTicks(long value)
AddYears Method datetime AddYears(int value)
You can call each of these methods to find a date/time in the future or in the past. Below you’ll see a few examples of invoking each of these methods and their output.
#Adding 8 days to the current date
PS> (Get-Date).AddDays(8)
Monday, November 30, 2020 1:59:39 AM
#Adding 3 hours to the current time
PS> (Get-Date).AddHours(3)
Sunday, November 22, 2020 4:59:51 AM
#Adding five years to the current date
PS> (Get-Date).AddYears(5)
Saturday, November 22, 2025 2:00:11 AM
#Subtracting 8 days from the current date using a negative number.
PS> (Get-Date).AddDays(-8)
Saturday, November 14, 2020 2:02:43 AM
Using New-Timespan
What if you wanted to know the difference between two dates; not just adding a specific number of time chunks? One way to do this is to use the New-Timespan cmdlet. The New-Timespan cmdlet generates TimeSpan objects which represent date/time differences or spans.
Use the New-TimeSpan cmdlet by providing a Start and End date/time as parameters. For example, if you’d like to find the difference between November 23rd, 2020 at midnight to December 31st, 2020 at midnight, you could use the following code snippet.
Using TimeSpan objects, you can find date/time differences in just about any increment you’d like.
PS> $StartDate = Get-Date -Month 11 -Day 23 -Year 2020 -Hour 00 -Minute 00 -Second 00
PS> $EndDate = Get-Date -Month 12 -Day 31 -Year 2020 -Hour 00 -Minute 00 -Second 00
PS> $StartDate
Monday, November 23, 2020 12:00:00 AM
PS> $EndDate
Thursday, December 31, 2020 12:00:00 AM
PS> New-TimeSpan -Start $StartDate -End $EndDate
Days : 38
Hours : 0
Minutes : 0
Seconds : 0
Milliseconds : 672
Ticks : 32832006722080
TotalDays : 38.0000077801852
TotalHours : 912.000186724444
TotalMinutes : 54720.0112034667
TotalSeconds : 3283200.672208
TotalMilliseconds : 3283200672.208
Comparing Dates
Not only can you find differences in dates and times but you can also compare them using standard PowerShell operators. PowerShell knows when a date is “less than” (earlier than) or “greater than” (later than) another date.
To compare dates, simply create two DateTime objects using PowerShell Get Date command or perhaps by casting strings with [DateTime] and then using standard PowerShell operators like lt or gt.
You can see a simple example of comparing dates below.
#Declaring the date
PS> $Date1 = (Get-Date -Month 11 -Day 14 -Year 2020)
PS> $Date2 = Get-Date
PS> $Date1 -lt $Date2
True
Converting (Casting) Strings to DateTime Objects
PowerShell can also display dates in various formats. For example, let’s say you have a CSV file or spreadsheet with various dates defined in the format you see below in the date_time_utc column. The format is currently YYYY-MM-DDThh:mm:ss.0000000Z.
You’d like to parse this file and find all of the rows with a date seven days or older.
PowerShell, by default, doesn’t know that 2020-09-07T13:35:08.4780000Z is a date and time; it just thinks it’s a simple string. To make PowerShell understand, you must first convert it to a DateTime object like Get-Date returns.
To cast a string to a DateTime object, preface the string (or variable) with [DateTime]. When you do this, PowerShell tries to interpret the string as a date and time then provides you with all of the properties and methods available on this object type.
PS> $Date = "2020-09-07T13:35:08.4780000Z"
PS> [DateTime]$Date
Monday, September 07, 2020 1:35:08 PM
Using PowerShell Get Date : PowerShell Date Formatting
Now that you are aware of the basic methods of using dates, it’s time to delve deeper into the topic. Let’s play around a bit more by using different formats to display dates.
Whenever you run the PowerShell Get Date command, you will see an output like Monday, September 07, 2020 1:35:08 PM. This output is actually a DateTime object with properties and methods; not a simple string. But, you can convert this DateTime object to a string and represent it differently using various date formatting methods.
DateTime Methods
One way to change up how a DateTime object is displayed is by using methods on the DateTime object. The DateTime object has four methods you can use to change up the formatting:
ToLongDateString()
ToShortDateString()
ToLongTimeString()
ToShortTimeString()
You can see an example of using the ToShortDateString() and ToShortTimeString() methods below.
PS> [DateTime]$Date = $Date
PS> $date.ToShortDateString() + " " + $date.ToShortTimeString()
9/7/2020 7:05 PM
The Format Parameter
One way to change the date format of a DateTime object is by using Get-Date to generate the object and the Format parameter to change the format. The Format parameter accepts a string of characters each representing how a date/time string should look.
You can see a few examples of the characters you can use with the Format parameter.
#To display full date pattern with short time pattern
PS> Get-Date -Format f
Sunday, November 22, 2020 4:26 AM
#To display full date pattern with long time pattern
PS> Get-Date -Format F
Sunday, November 22, 2020 4:26:31 AM
#To display short date pattern
PS> Get-Date -Format d
11/22/2020
#To display long date pattern
PS> Get-Date -Format D
Sunday, November 22, 2020
#Year month pattern
PS> Get-Date -Format Y
November 2020
## Combining format characters
PS> Get-Date -Format "dddd dd-MM-yyyy HH:mm K"
Sunday 22-11-2020 03:41 +05:30