Powershell

How to Get the Current Date in PowerShell?

PowerShell is Microsoft’s automation and scripting platform. It’s a .NET Framework-based scripting language as well as an interactive command environment. PowerShell consists of a set of commands that perform specific functions. Just like any reasonable programming language, PowerShell can accomplish a lot of tasks. Displaying the current date is another simple function that PowerShell does effortlessly.

Using the “Get-Date” command, you can find the current date, format dates, tomorrow’s date, and a lot more with PowerShell. It also offers you various options for showing dates in a particular manner. This article will show you how to get the current date in PowerShell using different methods. So let’s begin:

What is Get-Date in PowerShell?

The Get-Date cmdlet is a lightweight command used in PowerShell. This command returns a DateTime object that displays the current date or a custom date. Get-Date supports a variety of UNIX and .NET date and time formats. It can generate a date or time character string, which can then be sent to other programs or commands.

On your local system, utilize the Get-Date cmdlet for getting the current date and time:

> Get-Date

You can utilize Get-Date when you want to use “Date” as an attribute. The command will retrieve both the date and the time. It is set to show the system’s current date and time in the format “00:00:00” by default. The retrieved date and time settings depend on your system’s cultural settings:

Getting current date and time elements in PowerShell

You can use the “-DisplayHint” parameter in the “Get-Date” command to only get the current date of your local system. Do you want to give this strategy a shot? In your PowerShell, type the following command:

> Get-Date -DisplayHint Date

To only view the current time of your system, specify the “Time” attribute in the “-DisplayHint” parameter:

> Get-Date -DisplayHint Time

If you want to get the current date and time both in your PowerShell, then add “DateTime” in the same “Get-Date” command along with the “-DisplayHint” parameter:

> Get-Date -DisplayHint DateTime

It will show you the following output:

View the Get-Date Format-List

Although the “Get-Date” cmdlet appears only to return the current time and date, it can provide more information. Check out the format list of the “Get-Date” cmdlet to have an idea of what we are talking about:

> Get-Date | Format-List

Use UFormat Specifier for getting current date and time

UFormat specifiers are also used for output format customization. Following are some UFormat specifiers with their definitions:

%A“: This UFormat specifier will retrieve the day of the week with the full name.

%m”: Use this specifier to get the month number:

%d”: “%d” will show the month’s day in two digits.

%Y“: This UFormat specifier will show you the current year in four-digit format.

%R“: Use this specifier for displaying time in 24-hour format without seconds.

%Z“: “%Z” denotes a time zone offset from the Universal Time Coordinate (UTC).

In PowerShell, type this command to get the current time and date according to the specific UFormat:

> Get-Date -UFormat "%A %m/%d/%Y %R %Z"

This command will also return the output as a String object.

Use .Net format Specifier for getting current date and time

Get-Date” utilizes various format specifiers to customize its output, .Net is one of them. Now, let’s have a look at some .net specifiers:

dddd”: This specifier will get the day of the week (full name).

MM”: It will show you the month number.

dd”: “dd” will show the month’s day in two digits.

yyyy“: This .Net specifier will show you the current year in four-digit format.

HH:mm“: Use this specifier for displaying time in 24-hour format without seconds.

K”: This specifier denotes a time zone offset from the Universal Time Coordinate (UTC).

According to your .Net specifiers, write out this command in your PowerShell for getting the current date and time:

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

The execution of the above-given command will return a String.

Get a date’s day in PowerShell

PowerShell is also capable of displaying you a date’s day. For that, it utilizes three parameters: “-Year,” “-Month,” and “-Day.” For example, the below-given command will retrieve your Gregorian calendar’s numeric day of October 03, 2022.

gt; (Get-Date -Year 2022 -Month 10 -Day 03).DayOfYear

Adding Hours in Current system date in PowerShell

You can also perform the arithmetic operation of adding hours in the current date. Specify the total number of hours as a parameter in the “AddHours()” function.

> (get-date).AddHours(6)

Get Yesterday’s date in PowerShell

Utilize the “AddDays()” function and specify “-1” as a parameter for this function. Calling the “AddDays()” function with these specified settings will let you display Yesterday’s date in your PowerShell.

> (get-date).AddDays(-1)

Check whether a date is set for daylight savings time

In the following example, we’ll see if the date we’ve mentioned has been set for daylight savings time. The “DST” variable will store the value returned by the “Get-Date” cmdlet. After that, it will call the “IsDayLightSavingTime()” boolean function. This function will answer your question in True or False.

> $DST = Get-Date
> $DST.IsDaylightSavingTime()

More examples of Get-Date formats

Adding “-Format g” as a parameter in your Get-Date command will show you the current date and time in a short format:

> Get-Date -Format g

Utilize the “-Format t” option if you only want to get the current time in a short format:

> Get-Date -Format t

Similarly, you can also retrieve only the current date of your system in short format by using the “-Format d” option.

> Get-Date -Format d

It will return the date in a certain format:

Use the “U” format option to retrieve the current time and date in Universal Time format.

> Get-Date -Format U

Conclusion

Is there any method to get the current date in PowerShell? You will be amazed to know that answer to this question is simple than the question itself seems. PowerShell utilizes a Get-Date cmdlet to get the current date of your local system. This command is also used for formatting outputs that involve date and time. We have shown you various formats for getting the current date in PowerShell. Moreover, different customization methods are also provided in this article.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.