PowerShell: MSOnline Get LastDirSyncTime in Local Time Format

Get-MsolCompanyInformation cmdlet’s property LastDirSyncTime from the Microsoft MSOnline PowerShell Module shows the Time information in the global UTC Format.

Convert to Local Time using UTC Offset

To convert this Time information to our Local TimeZone Format we can use the following PowerShell commands:

[code language="powershell" title="LastDirSyncTime in Local TimeZone Format"]
$LastDirSyncTime = (Get-MsolCompanyInformation).LastDirSyncTime
$timeZoneOffset = ([TimeZoneInfo]::Local).BaseUtcOffset.TotalHours
$LocalLastDirSyncTime = $LastDirSyncTime.AddHours($timeZoneOffset)
[/code]

The problem with the above code is that it does not take into account the Daylight Savings Time (DST) Offset. Sometimes referred to in Europe as Summer- or Winter-time.

Convert to Local Time using UTC and DST Offset

To convert this Time Information to our Local TimeZone (DST) Format we can use the following PowerShell commands:

[code language="powershell" title="LastDirSyncTime in Local TimeZone (DST) Format"]
$LastDirSyncTime = (Get-MsolCompanyInformation).LastDirSyncTime
$strCurrentTimeZone = (Get-WmiObject win32_timezone).StandardName
$currentTimeZone = [System.TimeZoneInfo]::FindSystemTimeZoneById($strCurrentTimeZone)
$LocalLastDirSyncTime = [System.TimeZoneInfo]::ConvertTimeFromUtc($LastDirSyncTime, $currentTimeZone)
[/code]