You are reading the article Examples To Implement Powershell Get updated in September 2023 on the website Uyenanhthammy.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Examples To Implement Powershell Get
Introduction to PowerShell Get-DateGet-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.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Syntax:
Following are the syntax for PowerShell Get-Date:
Code:
(Get-Culture).DateTimeFormat
Output:
Parameters of PowerShell Get-DateThe default output of the command Get-Date is displayed in the below format.
Code:
get-date
Output:
Below parameters are used by Get-Date cmdlet to format the command output.
1. DateWhen 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.
2. YearWhen 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.
3. MonthWhen 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.
4. DayWhen 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.
5. HourWhen 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.
6. MinuteWhen 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.
7. SecondThis 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.
8. MillisecondThis 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.
9. DisplayHintThis 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-DateBelow are the examples that implement PowerShell Get-Date:
Example #1The get-date command will give you the current Date and Time of the system. A similar output can be fetched by the below commands.
Code:
Get-Date -DisplayHint DateTime
Output:
Example #2To 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.
Code:
(Get-Date).Date
Output:
2. Get-Date -DisplayHint Date: This command will display only the date of the system.
Code:
Get-Date -DisplayHint Date
Output:
3. To display the Time, below command can be used
Code:
Get-Date -DisplayHint Time
Output:
Example #3Below properties are supported by the Get-Date command.
Above each property has the following commands:
Code:
“Year : ” + $d.Year
Output:
Example #4To format the output using -Format parameter, Here, we will display Date in “MMddyyyy”
Code:
Get-Date -Format "MMddyyyy"
Output:
You can also put the separator between them, For example,
Code:
Get-Date -Format “MM/dd/yyyy”
Outputs:
All of the above outputs will be in String format.
Example #5To get the output including hours, minutes and seconds,
Code:
Get-Date -Format "ddMMyyyy HH:mm:ss"
Outputs:
Example #6You 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.
Code:
Get-Date -Year 2030
Output:
Example #7To change output in UFormat, The table mentioned below will describe UFormat.
Code:
Get-Date -UFormat "%m%d%Y"
Output:
Similarly, you can use different combinations below.
Example #8There are also methods available for Get-Date as below.
If you want to get the Date and time after 3 days,
Code:
(Get-Date).AddDays(+3)
Output:
To get the date/time of 2 days back,
Code:
(Get-Date).AddDays(-2)
Output:
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).
Code:
(Get-Date).IsDaylightSavingTime()
Output:
ConclusionGet-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.
Recommended ArticlesThis 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 –
You're reading Examples To Implement Powershell Get
Update the detailed information about Examples To Implement Powershell Get on the Uyenanhthammy.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!