Introduction to PowerShell Get-Date
Get-Date cmdlet in PowerShell is used to get the Date and Time of the System. It uses the local computer system’s date and time. Its data type is DateTime. The output format of the Date and Time depends on the computer’s cultural settings that can be determined by the below syntax and command.
Following are the syntax for PowerShell Get-Date:
[[-Date] <DateTime>] [-Year <Int32>] [-Month <Int32>] [-Day <Int32>] [-Hour <Int32>] [-Minute <Int32>] [-Second <Int32>] [-Millisecond <Int32>] [-DisplayHint <DisplayHintType>] [-Format <String>] [-UFormat <String>] [<CommonParameters>]
Parameters of PowerShell Get-Date
The default output of the command Get-Date is displayed in the below format.
Below parameters are used by Get-Date cmdlet to format the command output.
When you use Date as a property like (Get-Date). Date, it specifies the date and time both. It displays the time 00:00:00 by default along with the system’s current date. Time property will give you the current system time. The output will be the DateTime data type only. And when you specify –Date parameter and date followed by it, it displays the specific date. For example Get-Date –Date “01/01/2010 22:00:00”. This action will not change the date/time of the system.
When you specify Year as a property like (Get-Date). The year will provide the current system year. The output of the command is Int32 datatype. You can use –Year parameter to display the different year.
When Month is mentioned as property like (Get-Date). Month it will display the current system’s month as an output. The output will be in the Int32 data type format. You can use –Month parameter to display the different month. The value should be between 1 to 12.
When Day is specified as a property like (Get-Date).Day, it will display the current system’s day as an output. The output will be in the Int32 format. You can use –Day parameter to display a different day. The value should be between 1 to 31.
When Hour is specified as a property like (Get-Date).Hour, it will display the current system’s hour as an output. The output will be in the Int32 format. You can use –Hour parameter to display the different hours. The value should be between 1 to 24.
When Minute is specified as a property like (Get-Date).Minute, it will display the current system’s minute as an output. The output will be in the Int32 format. You can use –Minute parameter to display the different minutes. The values should be between 1 to 60.
This parameter is to set and display the seconds of the current system and output will be in Int32 format. When you set seconds, it will just a temporary display of the seconds not setting it permanently. The value should be 1 to 60.
This parameter is to set and display the millisecond of the current system and output will be in Int32 format. When you set milliseconds, it will just a temporary display of the seconds not the setting permanently. The value should be 1 to 999.
This parameter displays only Date or only Time or DateTime, depends on the specified argument.
Accepted values are,
- Date: Display only system current date.
- Time: Display the only system current time.
- DateTime: Display only system DateTime
Examples to Implement PowerShell Get-Date
Below are the examples that implement PowerShell Get-Date:
The get-date command will give you the current Date and Time of the system. A similar output can be fetched by the below commands.
Get-Date -DisplayHint DateTime
To display the Date, you can use two commands.
1. (Get-Date).Date: This command will display the date along with 00:00:00 or 12:00:00 time format.
2. Get-Date -DisplayHint Date: This command will display only the date of the system.
Get-Date -DisplayHint Date
3. To display the Time, below command can be used
Get-Date -DisplayHint Time
Below properties are supported by the Get-Date command.
Above each property has the following commands:
$d = Get-Date
"Date : " + $d.Date
"Day : " + $d.Day
"Day Of Week : "+ $d.DayOfWeek
"Day of Year : "+ $d.DayOfYear
"Hour : " + $d.Hour
"Kind : " + $d.Kind
"MilliScond : " + $d.Millisecond
"Minute : " +$d.Minute
"Second : " +$d.Second
"Ticks : " + $d.Ticks
"Time Of Day : " + $d.TimeOfDay
"Year : " + $d.Year
To format the output using -Format parameter, Here, we will display Date in “MMddyyyy”
Get-Date -Format "MMddyyyy"
You can also put the separator between them, For example,
Get-Date -Format "MM-dd-yyyy"
Get-Date -Format "MM/dd/yyyy"
All of the above outputs will be in String format.
To get the output including hours, minutes and seconds,
Get-Date -Format "ddMMyyyy HH:mm:ss"
You can use the Get-Date command to temporarily change in time or date. For example, the below command will display the year 2030 in output, the rest date and time will remain the same.
Get-Date -Year 2030
Similarly, you can change the day, hour, date, minute, seconds, etc.
To change output in UFormat, The table mentioned below will describe UFormat.
Get-Date -UFormat "%m%d%Y"
Similarly, you can use different combinations below.
There are also methods available for Get-Date as below.
If you want to get the Date and time after 3 days,
To get the date/time of 2 days back,
Similarly, you can use other methods to add/minus the hours, months, seconds, etc with the methods described in the snapshot above. You can also check if daylight saving is enabled for the current date with IsDaylightSavingTime() and output will be in the boolean format (True or False).
Get-Date is useful when formatting outputs that involve date and time. You can extract data by modifying dates as well, which is assigned to a temporary variable. To set the date of the system, use the Set-Date command.
This is a guide to PowerShell Get-Date. Here we discuss syntax, the parameters are used by Get-Date cmdlet and the examples that implement PowerShell Get-Date. You can also go through our other related articles to learn more –
- PowerShell Format-Table
- If Statement in PowerShell
- PowerShell Test-Path | Examples | Parameters
- Examples to Implement PowerShell New-Item
- PowerShell Rename-Item | How to Implement?
- Date in HTML with Examples
- Parameters of PowerShell Convert to String