PowerShell Get-Date Format
In this article, we will discuss and use PowerShell’s Get-Date Format (or Date Format) command. Date and times properties are used several times throughout the PowerShell scripts. Using the Get-Date cmdlet, we can retrieve the current date of our machine. The default format of date is defined on the local system on which we run the command. There are several formats and parameters are used with the Get-Date command. Here, we will only focus on the formatting and display the different date results.
Before performing all the Get-Date cmdlet operations on the PowerShell, we must know how to start the PowerShell tool (application).
Start PowerShell as an Administrator
There are various ways to start PowerShell as an Administrator; some of the basic ways are mentioned below:
1. Run PowerShell as an Administrator using Windows’s search bar
Step 1: Windows 10 operating system comes with a search field in the taskbar. Point the cursor in the search field and type “powershell” (or PowerShell).
Step 2: Based on the search result, right-click on the Windows PowerShell and Run as Administrator. It will take you to Windows PowerShell’s as administrator mode.
2. Run PowerShell as an Administrator using Run window
Step 1: Press the Windows + R keys simultaneously, and it will open a Run dialog box. Now, type the PowerShell in the Run dialog box and click on the OK button.
Step 2: Step 1 opens the normal PowerShell windows for the current user. Now, on the PowerShell windows, type the command start-process powershell -verb runas and press the “enter” key.
Step 3: The above command asks you to open Windows PowerShell as an administrator mode; click OK to allow, and it will bring the Windows PowerShell to the administrator.
Date Formatting Parameters
Followings are the different types of date format in PowerShell:
When to use Get-Date
The Get-Date command returns a DateTime object that shows the current date or date you specify on your local device. Let’s see the default format of the system when we use the Get-Date command.
The Get-Date command returns a date and time value in string type of various UNIX and .NET formats and moves the string to other cmdlets.
The format of output shown on your shell’s screen is based on your formatted computer settings. To view your device date settings, use the (Get-Culture).DateTimeFormat command; it will return different date and time formats.
How to use the Get-Date command
The command Get-Date returns the current date and time of the system, and the result is shown in the form of long-date and long-time formats.
If we use a Format-List cmdlet with Get-Date, we will see the full information that returns various dates and time formats. The command Get-Date returns a DateTime object. Let see the output of using the Get-Date | Format-List cmdlet:
The DisplayHint parameter defines the type of information we want to display on the screen. This parameter also returns the DateTime object; however, it has three different types of formats, including Date, Time, and DateTime. The -DisplayHint parameter uses anyone from Date, Time, or DateTime at a time.
- Date: it only returns the current date value.
- Time: it only returns a current time value.
- DateTime: it returns both the current date and time values.
Let see the output of using the followings cmdlets:
Using the -Format parameter in the PowerShell command, you can display date and time in a specific format that you specify. The -Format parameter accepts all values which are allowed in the Microsoft .Net framework and included in the DateTimeFormatInfo Class. There are different format options available that you can use to format your date and time results according to your requirement.
Note that the result returned by -Format parameter is not the DateTime object; instead, they will a String result. We are mentioned some of the standard format specifiers information and their short description, which are provided by Microsoft.
Standard Strings Format Specifiers
Some of the standard .NET strings format specifiers that are mostly used are defined below:
|dddd||returns a day of the week in full name|
|MM||returns month number|
|dd||returns a day of the month in 2 digits|
|yyyy||returns a year in 4-digit|
|HH:mm||returns a time in 24-hour format without seconds|
|K||returns the time zone offset from Universal Time Coordinate (UTC)|
Let see an example to retrieve the date and time in a .NET format specifier using the following – Format parameter and several .NET format specifiers:
Optional Format list