In PowerShell, you can get the current date and time using the Get-Date cmdlet and the timezone information using [System.TimeZoneInfo] class.
The following method shows how you can do it with syntax.
Method 1: Use Get-Date and TimeZone Info
$currentDate = Get-Date
$timeZoneInfo = [System.TimeZonInfo]::Local.StandardName
Write-Output "Current Date is: $currentDate and TimeZone: $timeZoneInfo"
This example will output the current date and time along with the timezone information.
The following example shows how to use this method to get date and timezone information.
Get Date and TimeZone Information in PowerShell
You can get the current date and time using the Get-Date and timezone information using the [System.TimeZoneInfo] class.
Here’s the PowerShell script.
# Get the current date $currentDate = Get-Date # get the system timezone information $timeZoneInfo = [System.TimeZoneInfo]::Local.StandardName # output the date and time along with timezone information Write-Output "Current Date is: $currentDate and TimeZone: $timeZoneInfo"
Output:
In the above PowerShell script, we define $currentDate variable that contains the current date retrieved using the Get-Date cmdlet.
We then use the [System.TimeZone] information class to get the timezone information on the given system.
Finally, we use the Write-Output cmdlet to output the current date and system timezone information as India Standard Time.
If we run the following command, it output the system timezone information.
PS C:\> [System.TimeZoneInfo]::Local
Id : India Standard Time
DisplayName : (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi
StandardName : India Standard Time
DaylightName : India Daylight Time
BaseUtcOffset : 05:30:00
SupportsDaylightSavingTime : False
In this example, our system is in the India Standard Timezone (IST).
Conclusion
I hope the above article on getting date and timezone information in PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.