Search code examples
powershelldatetimeregistry

Read Registery value and convert to date


I am trying to find the uptime of windows machine (last shutdown time) using following code:

$computernames = gc LegAservers.txt
foreach ($computername in $computernames) {
  $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
  $sKey = "System\CurrentControlSet\Control\Windows"
  $key = $baseKey.OpenSubKey($sKey)
  $kvalue = $key.GetValue("ShutdownTime")
  $kvalue
  "------------------------------------------"
  $baseKey.Close()
}

Output is:

13
152
105
75
132
217
208
1
------------------------------------------

I am unable to covert this $kvalue in DateTime format using:

[DateTime]::FromFileTime($kvalue)

Please suggest proper way to convert $kvalue in date and time format so that it can be understood by user.


Solution

  • $regKey = Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Windows
    $shutDown = $regKey.ShutdownTime
    $Int64Value = [System.BitConverter]::ToInt64($shutDown, 0)
    $date = [DateTime]::FromFileTime($Int64Value)
    
    $date