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:
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:
To only view the current time of your system, specify the “Time” attribute in the “-DisplayHint” parameter:
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:
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:
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:
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:
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.
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 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.
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.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:
Utilize the “-Format t” option if you only want to get the current time in a short format:
Similarly, you can also retrieve only the current date of your system in short format by using the “-Format d” option.
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.
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.