Powershell

How to use PowerShell Get-Date cmdlet

The Get-Date object in PowerShell is used to print the date and time. The Get-Date provides different methods, parameters, and format specifiers which can be used to get date and time in Mircosoft.NET and Unix format. Date and time can be modified by user-defined methods. With Get-Date one can convert the date and time from one to another format.

In this article, you will learn the basic commands about the Get-Date cmdlet and their purposes, which is commonly used in PowerShell.

How to use PowerShell Get-Date cmdlet?

With a number of different methods, parameters, and format specifiers, one can get date and time using PowerShell Get-Date cmdlet.

The following are some basic uses of the Get-Date cmdlet in PowerShell.

Example 1: To get the current date and time

In PowerShell the get-date cmdlet displays the current date and time in the console. Conventionally, PowerShell uses the upper case of each word but the command in lower case is also acceptable as shown in the following command. We have used both upper-case/lower-case letters to execute the Get-Date.

Get-Date

get-date

It can be observed from the above output that both formats are returning the same result.

Example 2: To get a formatted date and time

The Format-List cmdlet of PowerShell offers to get the formatted output. The following example pipes the Get-Date with Format-List to get the date and time in a formatted and detailed manner.

Get-Date | Format-List

Note: The Format-List cmdlet can be used with any method of the Get-Date cmdlet mentioned in examples/commands.

Example 3: To get only the current date or the current time

The “-DisplayHint” parameter is used to get the current date or current time. The “-DisplayHint” parameter accepts either Date or Time as a value. For instance, the following command will only print the current date and day:

Get-Date -DisplayHint Date

The result shows that with the above command only the current date and day will be displayed.

Example 4: To customize date and time format (.NET specifier)

The following syntax will print the current date and time with your customized format such as:

Get-Date -Format "dddd MM/dd/yyyy HH:mm K"

The below-attached screenshot elaborates the syntax of the .NET format.

Example 5: To convert date and time to UTC

If you want to display the date and time in Universal Time format, first display the date and time in .NET or Uformat. For example, use the following command for .NET format specifier.

Get-Date -Format "dddd MM/dd/yyyy HH:mm K"

Let’s see how the current time is converted to the UTC (Coordinated Universal Time)

Assign a $Time variable to save the date and time.

$Time = Get-Date

Now, convert the time to a universal time format by using the “.ToUniversalTime()” method.

$Time.ToUniversalTime()

As per the above screenshot, it can be observed that date and time before conversion is totally different from the data and time after conversion.

Example 6: To convert the current date/time to string

The following command will be used to convert the date and time to a string object.

(Get-Date).ToString()

You can also define/set your date and time in user-defined format.

(get-date).ToString("d-M-yyyy hh:mm tt")

The output result shows that the command will return the same format that a user has already set.

Example 7: To convert unix time to current time zone

You can convert unix (time in seconds) to the current time zone with the below command.

Get-Date -UnixTimeSeconds 1654606074

It can be observed in the above screenshot that the time in seconds has been converted to your current timezone.

Example 8: To get a specific date and time

Sometimes you need to know the day ten (10) years ago. The following common will print the date and time ten (10) years ago:

Get-Date -Date "6/7/2012 12:30:22"

You can see that ten years ago (i.e., 2012) it was Thursday of 7th June.

Example 9: To Know about a specific day and month of a current year

With the parameters -Month and -Day, you can print the date and time of the specific Day and Month. For example, if I want to know about the day of 1st January, the following command will be used for the purpose.

Get-Date -Month 1 -Day 1

The above result displayed the name of the day of 1st January along with the current year and time.

Example 10: To add years to the date and time

With the help of the Get-Date cmdlet, you can add more years to the current date. For instance, run the below command in your PowerShell.

(Get-Date).AddYears(1)

As you can see in the above example, after a year it will be Friday on the same date and time.

You can also subtract the number of days from the current date with the AddDays() method. Here is a practical example:

Get-Date

(Get-Date).AddDays(-10)

The above output shows that the current date is June 9, while ten (10) days before it was the 30th of May 2022.

Example 11: To format the date and time format (UFormat specifier)

The –UFormat is also used for date and time customization. The “UFormat” parameter prints the date/time in a specific format. There are numerous format specifiers that serve a specific purpose. For instance, The following syntax is associated with the UFormat specifier.

Get-Date -UFormat "%A %B/%d/%Y %T %Z"

The above output shows that the command will print the name of day, month, day, year, hour and time zone accordingly.

Example 12: To convert date and time to unix time (seconds)

Powershell has also facilitated converting your current date and time to unix time. Unix time introduced a new date-time format which is used to show the date and time in seconds. Unix format basically counts the date and time in seconds from the period of January 1, 1970, 00:00:00.

Get-Date -UFormat "%s"

The output converts the current date and time into seconds which states the number of seconds elapsed from 1970 at the time of executing the command.

Example 13: To get the Current Century

The “%C” format specifier is used to show the name/number of the current Century.

Get-Date -UFormat "%C"

As you can see in the above output, the current century we are living in is the 20th Century.

Example 14: To find out the current week of the year

The below-mentioned format specifier “%Y %V” is helpful to display the current week of the year.

Get-Date -UFormat "%Y %V"

As shown in the above screenshot, currently it’s the 23rd week of the current year 2022.

Note: The examples 10, 11, 12, and 13 refer to the usage of the format specifiers.

Bonus Tip

There are lots more functions of the Get-Date cmdlet. The Bonus Tip is here for you to explore more about Get-Date.

The below commands will help you to learn more about the Get-Date command.

Get-Date | Get-Member

Help Get-Date

For more help with the syntax and work of the command, run the below Help command.

Help Get-Date

Here you go! You have learned to use the Get-Date cmdlet in various scenarios of PowerShell.

Conclusion

The Get-Date cmdlet in PowerShell is used to print date and time in various formats. In this article, you have learned the basic Get-Date commands and their purposes, which are commonly used in PowerShell. The Get-Date cmdlet offers several operators to print the date, time, time zones, and more information in a variety of formats.

About the author

Adnan Shabbir