Display current time with time zone in PowerShell
I'm trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:
Time: 8:00:34 AM EST
I'm currently using the following script:
$localtz = [System.TimeZoneInfo]::Local | Select-Object -expandproperty Id
if ($localtz -match "Eastern") {$x = " EST"}
if ($localtz -match "Pacific") {$x = " PST"}
if ($localtz -match "Central") {$x = " CST"}
"Time: " + (Get-Date).Hour + ":" + (Get-Date).Minute + ":" + (Get-Date).Second + $x
I'd like to be able to display the time without relying on simple logic, but be able to give the local timezone on any system.
powershell time timezone
add a comment |
I'm trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:
Time: 8:00:34 AM EST
I'm currently using the following script:
$localtz = [System.TimeZoneInfo]::Local | Select-Object -expandproperty Id
if ($localtz -match "Eastern") {$x = " EST"}
if ($localtz -match "Pacific") {$x = " PST"}
if ($localtz -match "Central") {$x = " CST"}
"Time: " + (Get-Date).Hour + ":" + (Get-Date).Minute + ":" + (Get-Date).Second + $x
I'd like to be able to display the time without relying on simple logic, but be able to give the local timezone on any system.
powershell time timezone
add a comment |
I'm trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:
Time: 8:00:34 AM EST
I'm currently using the following script:
$localtz = [System.TimeZoneInfo]::Local | Select-Object -expandproperty Id
if ($localtz -match "Eastern") {$x = " EST"}
if ($localtz -match "Pacific") {$x = " PST"}
if ($localtz -match "Central") {$x = " CST"}
"Time: " + (Get-Date).Hour + ":" + (Get-Date).Minute + ":" + (Get-Date).Second + $x
I'd like to be able to display the time without relying on simple logic, but be able to give the local timezone on any system.
powershell time timezone
I'm trying to display the local time on my system with the TimeZone. How can I display time in this format the simplest way possible on any system?:
Time: 8:00:34 AM EST
I'm currently using the following script:
$localtz = [System.TimeZoneInfo]::Local | Select-Object -expandproperty Id
if ($localtz -match "Eastern") {$x = " EST"}
if ($localtz -match "Pacific") {$x = " PST"}
if ($localtz -match "Central") {$x = " CST"}
"Time: " + (Get-Date).Hour + ":" + (Get-Date).Minute + ":" + (Get-Date).Second + $x
I'd like to be able to display the time without relying on simple logic, but be able to give the local timezone on any system.
powershell time timezone
powershell time timezone
edited Jan 1 at 22:55


Peter Mortensen
13.8k1986113
13.8k1986113
asked Jun 14 '12 at 15:00
Ken JKen J
1,68353853
1,68353853
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:
[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
My regular expression probably leaves something to be desired.
The output of the above for my time zone is EST
. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime
and TimeZoneInfo
, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName
.
EDIT: I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo
for GMT+12
looks like this:
PS> [TimeZoneInfo]::Local
Id : UTC+12
DisplayName : (GMT+12:00) Coordinated Universal Time+12
StandardName : UTC+12
DaylightName : UTC+12
BaseUtcOffset : 12:00:00
SupportsDaylightSavingTime : False
Which produces this result for my code:
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
U+12
So, I guess you'd have to detect whether the StandardName
appears to be a set of words or just offset designation because there's no standard name for it.
The less problematic ones outside the US appear to follow the three-word format:
PS> [TimeZoneInfo]::Local
Id : Tokyo Standard Time
DisplayName : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Daylight Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
TST
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
SA Pacific Standard Time
becomesSPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset:[System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
add a comment |
You should look into DateTime
format strings. Although I'm not sure they can return a time zone short name, you can easily get an offset from UTC.
$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date)
This returns:
8:00:34 AM -04:00
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
add a comment |
Be loath to define another datetime format! Use an existing one, such as RFC 1123. There's even a PowerShell shortcut!
Get-Date -format r
Thu, 14 Jun 2012 16:44:18 GMT
Ref.: Get-Date
5
Should be noted that this is not very accurate: currently reportsGMT
when it is reallyBST
(one hour ahead of GMT).
– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
add a comment |
I'm not aware of any object that can do the work for you. You could wrap the logic in a function:
function Get-MyDate{
$tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
Eastern {'EST'; break}
Pacific {'PST'; break}
Central {'CST'; break}
}
"Time: {0:T} $tz" -f (Get-Date)
}
Get-MyDate
Or even take the initials of the time zone id:
$tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
"Time: {0:T} $tz" -f (Get-Date)
Remember to callIsDaylightSavingTime
to check whether to look at theStandardName
(I think this is the same as theId
, but unclear if always true) orDaylightName
property. Also you need to handle the default case for all the other timezones.
– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
add a comment |
This is a better answer:
$A = Get-Date #Returns local date/time
$B = $A.ToUniversalTime() #Convert it to UTC
# Figure out your current offset from UTC
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset
#Add the Offset
$C = $B + $Offset.BaseUtcOffset
$C.ToString()
Output:
3/20/2017 11:55:55 PM
add a comment |
I just combined several scripts and finally was able to run the script in my domain controller.
The script provides the output of time and timezone for all the machines connected under the domain.
We had a major issue with our application servers and used this script to cross check the time and timezone.
# The below scripts provides the time and time zone for the connected machines in a domain
# Appends the output to a text file with the time stamp
# Checks if the host is reachable or not via a ping command
Start-Transcript -path C:output.txt -append
$ldapSearcher = New-Object directoryservices.directorysearcher;
$ldapSearcher.filter = "(objectclass=computer)";
$computers = $ldapSearcher.findall();
foreach ($computer in $computers)
{
$compname = $computer.properties["name"]
$ping = gwmi win32_pingstatus -f "Address = '$compname'"
$compname
if ($ping.statuscode -eq 0)
{
try
{
$ErrorActionPreference = "Stop"
Write-Host “Attempting to determine timezone information for $compname…”
$Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
$remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
[datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
if ($Timezone)
{
foreach ($item in $Timezone)
{
$TZDescription = $Timezone.Description
$TZDaylightTime = $Timezone.DaylightName
$TZStandardTime = $Timezone.StandardName
$TZStandardTime = $Timezone.StandardTime
}
Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
}
else
{
Write-Host ("Something went wrong")
}
}
catch
{
Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
}
finally
{
$ErrorActionPreference = "Continue"
}
}
else
{
Write-Host ("Host $compname is not reachable from ping `n")
}
}
Stop-Transcript
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11035905%2fdisplay-current-time-with-time-zone-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:
[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
My regular expression probably leaves something to be desired.
The output of the above for my time zone is EST
. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime
and TimeZoneInfo
, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName
.
EDIT: I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo
for GMT+12
looks like this:
PS> [TimeZoneInfo]::Local
Id : UTC+12
DisplayName : (GMT+12:00) Coordinated Universal Time+12
StandardName : UTC+12
DaylightName : UTC+12
BaseUtcOffset : 12:00:00
SupportsDaylightSavingTime : False
Which produces this result for my code:
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
U+12
So, I guess you'd have to detect whether the StandardName
appears to be a set of words or just offset designation because there's no standard name for it.
The less problematic ones outside the US appear to follow the three-word format:
PS> [TimeZoneInfo]::Local
Id : Tokyo Standard Time
DisplayName : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Daylight Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
TST
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
SA Pacific Standard Time
becomesSPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset:[System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
add a comment |
While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:
[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
My regular expression probably leaves something to be desired.
The output of the above for my time zone is EST
. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime
and TimeZoneInfo
, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName
.
EDIT: I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo
for GMT+12
looks like this:
PS> [TimeZoneInfo]::Local
Id : UTC+12
DisplayName : (GMT+12:00) Coordinated Universal Time+12
StandardName : UTC+12
DaylightName : UTC+12
BaseUtcOffset : 12:00:00
SupportsDaylightSavingTime : False
Which produces this result for my code:
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
U+12
So, I guess you'd have to detect whether the StandardName
appears to be a set of words or just offset designation because there's no standard name for it.
The less problematic ones outside the US appear to follow the three-word format:
PS> [TimeZoneInfo]::Local
Id : Tokyo Standard Time
DisplayName : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Daylight Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
TST
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
SA Pacific Standard Time
becomesSPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset:[System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
add a comment |
While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:
[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
My regular expression probably leaves something to be desired.
The output of the above for my time zone is EST
. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime
and TimeZoneInfo
, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName
.
EDIT: I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo
for GMT+12
looks like this:
PS> [TimeZoneInfo]::Local
Id : UTC+12
DisplayName : (GMT+12:00) Coordinated Universal Time+12
StandardName : UTC+12
DaylightName : UTC+12
BaseUtcOffset : 12:00:00
SupportsDaylightSavingTime : False
Which produces this result for my code:
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
U+12
So, I guess you'd have to detect whether the StandardName
appears to be a set of words or just offset designation because there's no standard name for it.
The less problematic ones outside the US appear to follow the three-word format:
PS> [TimeZoneInfo]::Local
Id : Tokyo Standard Time
DisplayName : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Daylight Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
TST
While this is a bit ... naive perhaps, it's one way to get an abbreviation without a switch statement:
[Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
My regular expression probably leaves something to be desired.
The output of the above for my time zone is EST
. I did some looking as I wanted to see what the value would be for other GMT offset settings, but .NET doesn't seem to have very good links between DateTime
and TimeZoneInfo
, so I couldn't just programmatically run through them all to check. This might not work properly for some of the strings that come back for StandardName
.
EDIT: I did some more investigation changing the time zone on my computer manually to check this and a TimeZoneInfo
for GMT+12
looks like this:
PS> [TimeZoneInfo]::Local
Id : UTC+12
DisplayName : (GMT+12:00) Coordinated Universal Time+12
StandardName : UTC+12
DaylightName : UTC+12
BaseUtcOffset : 12:00:00
SupportsDaylightSavingTime : False
Which produces this result for my code:
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
U+12
So, I guess you'd have to detect whether the StandardName
appears to be a set of words or just offset designation because there's no standard name for it.
The less problematic ones outside the US appear to follow the three-word format:
PS> [TimeZoneInfo]::Local
Id : Tokyo Standard Time
DisplayName : (GMT+09:00) Osaka, Sapporo, Tokyo
StandardName : Tokyo Standard Time
DaylightName : Tokyo Daylight Time
BaseUtcOffset : 09:00:00
SupportsDaylightSavingTime : False
PS> [Regex]::Replace([System.TimeZoneInfo]::Local.StandardName, '([A-Z])w+s*', '$1')
TST
edited Jun 14 '12 at 19:58
answered Jun 14 '12 at 19:48
ShibumiShibumi
955417
955417
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
SA Pacific Standard Time
becomesSPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset:[System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
add a comment |
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
SA Pacific Standard Time
becomesSPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset:[System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
Works like a beauty!
– Ken J
Jun 15 '12 at 22:13
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
@Ken I was a little embarassed at it, but I'm glad it did the trick :).
– Shibumi
Jun 18 '12 at 12:54
1
1
SA Pacific Standard Time
becomes SPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset: [System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
SA Pacific Standard Time
becomes SPST
; which isn't really accurate. The information you gave was very helpful though. I decided to just settle on getting the minute offset: [System.TimeZone]::CurrentTimeZone.GetUtcOffset([datetime]::Now).TotalMinutes
– VertigoRay
Aug 29 '16 at 18:16
add a comment |
You should look into DateTime
format strings. Although I'm not sure they can return a time zone short name, you can easily get an offset from UTC.
$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date)
This returns:
8:00:34 AM -04:00
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
add a comment |
You should look into DateTime
format strings. Although I'm not sure they can return a time zone short name, you can easily get an offset from UTC.
$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date)
This returns:
8:00:34 AM -04:00
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
add a comment |
You should look into DateTime
format strings. Although I'm not sure they can return a time zone short name, you can easily get an offset from UTC.
$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date)
This returns:
8:00:34 AM -04:00
You should look into DateTime
format strings. Although I'm not sure they can return a time zone short name, you can easily get an offset from UTC.
$formatteddate = "{0:h:mm:ss tt zzz}" -f (get-date)
This returns:
8:00:34 AM -04:00
answered Jun 14 '12 at 15:34


Robbie RosatiRobbie Rosati
8051721
8051721
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
add a comment |
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
I appreciate the help with formatting. However, I still have to figure out a way to associate the Time Zone.
– Ken J
Jun 14 '12 at 15:56
add a comment |
Be loath to define another datetime format! Use an existing one, such as RFC 1123. There's even a PowerShell shortcut!
Get-Date -format r
Thu, 14 Jun 2012 16:44:18 GMT
Ref.: Get-Date
5
Should be noted that this is not very accurate: currently reportsGMT
when it is reallyBST
(one hour ahead of GMT).
– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
add a comment |
Be loath to define another datetime format! Use an existing one, such as RFC 1123. There's even a PowerShell shortcut!
Get-Date -format r
Thu, 14 Jun 2012 16:44:18 GMT
Ref.: Get-Date
5
Should be noted that this is not very accurate: currently reportsGMT
when it is reallyBST
(one hour ahead of GMT).
– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
add a comment |
Be loath to define another datetime format! Use an existing one, such as RFC 1123. There's even a PowerShell shortcut!
Get-Date -format r
Thu, 14 Jun 2012 16:44:18 GMT
Ref.: Get-Date
Be loath to define another datetime format! Use an existing one, such as RFC 1123. There's even a PowerShell shortcut!
Get-Date -format r
Thu, 14 Jun 2012 16:44:18 GMT
Ref.: Get-Date
edited Jan 1 at 23:45


Peter Mortensen
13.8k1986113
13.8k1986113
answered Jun 14 '12 at 15:45
Colonel PanicColonel Panic
81.8k61305395
81.8k61305395
5
Should be noted that this is not very accurate: currently reportsGMT
when it is reallyBST
(one hour ahead of GMT).
– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
add a comment |
5
Should be noted that this is not very accurate: currently reportsGMT
when it is reallyBST
(one hour ahead of GMT).
– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
5
5
Should be noted that this is not very accurate: currently reports
GMT
when it is really BST
(one hour ahead of GMT).– Richard
Jun 14 '12 at 15:52
Should be noted that this is not very accurate: currently reports
GMT
when it is really BST
(one hour ahead of GMT).– Richard
Jun 14 '12 at 15:52
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
But this is not correct as the current Time Zone is not GMT. I had previously using format but it always gives GMT instead of the correct Time Zone.
– Ken J
Jun 14 '12 at 15:57
6
6
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
You get GMT regardless of your time zone, it is hard coded - (Get-Culture).DateTimeFormat.RFC1123Pattern,
– Shay Levy
Jun 14 '12 at 16:00
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
Woah! That is dreadful. Thanks for the warning.
– Colonel Panic
Jun 14 '12 at 16:10
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
-format u gives you the correct universal time, the '-g' API is broken IMO
– cmcginty
May 28 '14 at 0:30
add a comment |
I'm not aware of any object that can do the work for you. You could wrap the logic in a function:
function Get-MyDate{
$tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
Eastern {'EST'; break}
Pacific {'PST'; break}
Central {'CST'; break}
}
"Time: {0:T} $tz" -f (Get-Date)
}
Get-MyDate
Or even take the initials of the time zone id:
$tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
"Time: {0:T} $tz" -f (Get-Date)
Remember to callIsDaylightSavingTime
to check whether to look at theStandardName
(I think this is the same as theId
, but unclear if always true) orDaylightName
property. Also you need to handle the default case for all the other timezones.
– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
add a comment |
I'm not aware of any object that can do the work for you. You could wrap the logic in a function:
function Get-MyDate{
$tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
Eastern {'EST'; break}
Pacific {'PST'; break}
Central {'CST'; break}
}
"Time: {0:T} $tz" -f (Get-Date)
}
Get-MyDate
Or even take the initials of the time zone id:
$tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
"Time: {0:T} $tz" -f (Get-Date)
Remember to callIsDaylightSavingTime
to check whether to look at theStandardName
(I think this is the same as theId
, but unclear if always true) orDaylightName
property. Also you need to handle the default case for all the other timezones.
– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
add a comment |
I'm not aware of any object that can do the work for you. You could wrap the logic in a function:
function Get-MyDate{
$tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
Eastern {'EST'; break}
Pacific {'PST'; break}
Central {'CST'; break}
}
"Time: {0:T} $tz" -f (Get-Date)
}
Get-MyDate
Or even take the initials of the time zone id:
$tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
"Time: {0:T} $tz" -f (Get-Date)
I'm not aware of any object that can do the work for you. You could wrap the logic in a function:
function Get-MyDate{
$tz = switch -regex ([System.TimeZoneInfo]::Local.Id){
Eastern {'EST'; break}
Pacific {'PST'; break}
Central {'CST'; break}
}
"Time: {0:T} $tz" -f (Get-Date)
}
Get-MyDate
Or even take the initials of the time zone id:
$tz = -join ([System.TimeZoneInfo]::Local.Id.Split() | Foreach-Object {$_[0]})
"Time: {0:T} $tz" -f (Get-Date)
edited Jan 1 at 22:58


Peter Mortensen
13.8k1986113
13.8k1986113
answered Jun 14 '12 at 15:26
Shay LevyShay Levy
87.6k17140157
87.6k17140157
Remember to callIsDaylightSavingTime
to check whether to look at theStandardName
(I think this is the same as theId
, but unclear if always true) orDaylightName
property. Also you need to handle the default case for all the other timezones.
– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
add a comment |
Remember to callIsDaylightSavingTime
to check whether to look at theStandardName
(I think this is the same as theId
, but unclear if always true) orDaylightName
property. Also you need to handle the default case for all the other timezones.
– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
Remember to call
IsDaylightSavingTime
to check whether to look at the StandardName
(I think this is the same as the Id
, but unclear if always true) or DaylightName
property. Also you need to handle the default case for all the other timezones.– Richard
Jun 14 '12 at 15:56
Remember to call
IsDaylightSavingTime
to check whether to look at the StandardName
(I think this is the same as the Id
, but unclear if always true) or DaylightName
property. Also you need to handle the default case for all the other timezones.– Richard
Jun 14 '12 at 15:56
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
@Richard What's wrong with the default case?
– Shay Levy
Jun 14 '12 at 16:07
There isn't one.
– Richard
Jun 14 '12 at 19:38
There isn't one.
– Richard
Jun 14 '12 at 19:38
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
Gotcha, no biggie, in this case you'll get just the time (unless you want to print a default value) :)
– Shay Levy
Jun 14 '12 at 20:02
add a comment |
This is a better answer:
$A = Get-Date #Returns local date/time
$B = $A.ToUniversalTime() #Convert it to UTC
# Figure out your current offset from UTC
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset
#Add the Offset
$C = $B + $Offset.BaseUtcOffset
$C.ToString()
Output:
3/20/2017 11:55:55 PM
add a comment |
This is a better answer:
$A = Get-Date #Returns local date/time
$B = $A.ToUniversalTime() #Convert it to UTC
# Figure out your current offset from UTC
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset
#Add the Offset
$C = $B + $Offset.BaseUtcOffset
$C.ToString()
Output:
3/20/2017 11:55:55 PM
add a comment |
This is a better answer:
$A = Get-Date #Returns local date/time
$B = $A.ToUniversalTime() #Convert it to UTC
# Figure out your current offset from UTC
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset
#Add the Offset
$C = $B + $Offset.BaseUtcOffset
$C.ToString()
Output:
3/20/2017 11:55:55 PM
This is a better answer:
$A = Get-Date #Returns local date/time
$B = $A.ToUniversalTime() #Convert it to UTC
# Figure out your current offset from UTC
$Offset = [TimeZoneInfo]::Local | Select BaseUtcOffset
#Add the Offset
$C = $B + $Offset.BaseUtcOffset
$C.ToString()
Output:
3/20/2017 11:55:55 PM
edited Mar 21 '17 at 7:03
answered Mar 21 '17 at 4:46
Gavin StevensGavin Stevens
460213
460213
add a comment |
add a comment |
I just combined several scripts and finally was able to run the script in my domain controller.
The script provides the output of time and timezone for all the machines connected under the domain.
We had a major issue with our application servers and used this script to cross check the time and timezone.
# The below scripts provides the time and time zone for the connected machines in a domain
# Appends the output to a text file with the time stamp
# Checks if the host is reachable or not via a ping command
Start-Transcript -path C:output.txt -append
$ldapSearcher = New-Object directoryservices.directorysearcher;
$ldapSearcher.filter = "(objectclass=computer)";
$computers = $ldapSearcher.findall();
foreach ($computer in $computers)
{
$compname = $computer.properties["name"]
$ping = gwmi win32_pingstatus -f "Address = '$compname'"
$compname
if ($ping.statuscode -eq 0)
{
try
{
$ErrorActionPreference = "Stop"
Write-Host “Attempting to determine timezone information for $compname…”
$Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
$remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
[datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
if ($Timezone)
{
foreach ($item in $Timezone)
{
$TZDescription = $Timezone.Description
$TZDaylightTime = $Timezone.DaylightName
$TZStandardTime = $Timezone.StandardName
$TZStandardTime = $Timezone.StandardTime
}
Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
}
else
{
Write-Host ("Something went wrong")
}
}
catch
{
Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
}
finally
{
$ErrorActionPreference = "Continue"
}
}
else
{
Write-Host ("Host $compname is not reachable from ping `n")
}
}
Stop-Transcript
add a comment |
I just combined several scripts and finally was able to run the script in my domain controller.
The script provides the output of time and timezone for all the machines connected under the domain.
We had a major issue with our application servers and used this script to cross check the time and timezone.
# The below scripts provides the time and time zone for the connected machines in a domain
# Appends the output to a text file with the time stamp
# Checks if the host is reachable or not via a ping command
Start-Transcript -path C:output.txt -append
$ldapSearcher = New-Object directoryservices.directorysearcher;
$ldapSearcher.filter = "(objectclass=computer)";
$computers = $ldapSearcher.findall();
foreach ($computer in $computers)
{
$compname = $computer.properties["name"]
$ping = gwmi win32_pingstatus -f "Address = '$compname'"
$compname
if ($ping.statuscode -eq 0)
{
try
{
$ErrorActionPreference = "Stop"
Write-Host “Attempting to determine timezone information for $compname…”
$Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
$remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
[datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
if ($Timezone)
{
foreach ($item in $Timezone)
{
$TZDescription = $Timezone.Description
$TZDaylightTime = $Timezone.DaylightName
$TZStandardTime = $Timezone.StandardName
$TZStandardTime = $Timezone.StandardTime
}
Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
}
else
{
Write-Host ("Something went wrong")
}
}
catch
{
Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
}
finally
{
$ErrorActionPreference = "Continue"
}
}
else
{
Write-Host ("Host $compname is not reachable from ping `n")
}
}
Stop-Transcript
add a comment |
I just combined several scripts and finally was able to run the script in my domain controller.
The script provides the output of time and timezone for all the machines connected under the domain.
We had a major issue with our application servers and used this script to cross check the time and timezone.
# The below scripts provides the time and time zone for the connected machines in a domain
# Appends the output to a text file with the time stamp
# Checks if the host is reachable or not via a ping command
Start-Transcript -path C:output.txt -append
$ldapSearcher = New-Object directoryservices.directorysearcher;
$ldapSearcher.filter = "(objectclass=computer)";
$computers = $ldapSearcher.findall();
foreach ($computer in $computers)
{
$compname = $computer.properties["name"]
$ping = gwmi win32_pingstatus -f "Address = '$compname'"
$compname
if ($ping.statuscode -eq 0)
{
try
{
$ErrorActionPreference = "Stop"
Write-Host “Attempting to determine timezone information for $compname…”
$Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
$remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
[datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
if ($Timezone)
{
foreach ($item in $Timezone)
{
$TZDescription = $Timezone.Description
$TZDaylightTime = $Timezone.DaylightName
$TZStandardTime = $Timezone.StandardName
$TZStandardTime = $Timezone.StandardTime
}
Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
}
else
{
Write-Host ("Something went wrong")
}
}
catch
{
Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
}
finally
{
$ErrorActionPreference = "Continue"
}
}
else
{
Write-Host ("Host $compname is not reachable from ping `n")
}
}
Stop-Transcript
I just combined several scripts and finally was able to run the script in my domain controller.
The script provides the output of time and timezone for all the machines connected under the domain.
We had a major issue with our application servers and used this script to cross check the time and timezone.
# The below scripts provides the time and time zone for the connected machines in a domain
# Appends the output to a text file with the time stamp
# Checks if the host is reachable or not via a ping command
Start-Transcript -path C:output.txt -append
$ldapSearcher = New-Object directoryservices.directorysearcher;
$ldapSearcher.filter = "(objectclass=computer)";
$computers = $ldapSearcher.findall();
foreach ($computer in $computers)
{
$compname = $computer.properties["name"]
$ping = gwmi win32_pingstatus -f "Address = '$compname'"
$compname
if ($ping.statuscode -eq 0)
{
try
{
$ErrorActionPreference = "Stop"
Write-Host “Attempting to determine timezone information for $compname…”
$Timezone = Get-WMIObject -class Win32_TimeZone -ComputerName $compname
$remoteOSInfo = gwmi win32_OperatingSystem -computername $compname
[datetime]$remoteDateTime = $remoteOSInfo.convertToDatetime($remoteOSInfo.LocalDateTime)
if ($Timezone)
{
foreach ($item in $Timezone)
{
$TZDescription = $Timezone.Description
$TZDaylightTime = $Timezone.DaylightName
$TZStandardTime = $Timezone.StandardName
$TZStandardTime = $Timezone.StandardTime
}
Write-Host "Timezone is set to $TZDescription`nTime and Date is $remoteDateTime`n**********************`n"
}
else
{
Write-Host ("Something went wrong")
}
}
catch
{
Write-Host ("You have insufficient rights to query the computer or the RPC server is not available.")
}
finally
{
$ErrorActionPreference = "Continue"
}
}
else
{
Write-Host ("Host $compname is not reachable from ping `n")
}
}
Stop-Transcript
edited Jan 1 at 23:50


Peter Mortensen
13.8k1986113
13.8k1986113
answered Jul 8 '16 at 12:17
Raghav SRaghav S
32625
32625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11035905%2fdisplay-current-time-with-time-zone-in-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown