{"id":1675,"date":"2022-02-18T06:23:40","date_gmt":"2022-02-18T14:23:40","guid":{"rendered":"https:\/\/tungle.ca\/?p=1675"},"modified":"2022-10-22T08:23:34","modified_gmt":"2022-10-22T16:23:34","slug":"get-lastreboot-and-uptime-value-on-windows-10-and-windows-server-via-ps","status":"publish","type":"post","link":"https:\/\/tungle.ca\/?p=1675","title":{"rendered":"Get LastReboot and Uptime value on Windows 10 and Windows Server via PS"},"content":{"rendered":"\n<p>In this lab, I have used PS to get LastReboot and Uptime value on Windows servers and clients. PS script will query &#8220;Get-WmiObject&#8221; to get the last boot time of the computers\/servers.<\/p>\n\n\n\n<p>Firstly, I will test with Windows Servers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$OS1 = Get-WmiObject Win32_OperatingSystem -ComputerName DC1 -ErrorAction Stop\n$LastReboot =($OS1.ConvertToDateTime($OS1.LastBootUpTime))\n# Print LastReboot\n$LastReboot\n$Uptime = (Get-Date) -$OS1.ConvertToDateTime($OS1.LastBootUpTime)\n$Uptime  = ([String]$Uptime.Days + \" Days \" + $Uptime.Hours + \" Hours \" + $Uptime.Minutes + \" Minutes\")\n# Print uptime<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"986\" height=\"151\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-241.png\" alt=\"\" class=\"wp-image-1676\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-241.png 986w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-241-300x46.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-241-768x118.png 768w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-241-200x31.png 200w\" sizes=\"auto, (max-width: 986px) 100vw, 986px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"879\" height=\"300\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-242.png\" alt=\"\" class=\"wp-image-1677\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-242.png 879w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-242-300x102.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-242-768x262.png 768w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-242-200x68.png 200w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/figure>\n\n\n\n<p>Write a full PS script.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\"># This script is written by Tung on 2022-02-18\n# This is used to get lastreboot and uptime on Windows servers.\n# Get time when running the script\n$filename = (Get-Date).tostring(\"dd-MM-yyyy-hh-mm\")\n# Change PowerShell working directory to C:\\Shared\nSet-Location C:\\Shared\n#$TestComputerName = get-content C:\\Shared\\tungmachine.txt\n$servers = (Get-ADComputer -properties OperatingSystem -filter{(operatingsystem -like \"*Windows Server*\")}).name\nForeach ($server in $servers) {\n        # Only check the machine if it is online\n        $ping_result = Test-Connection -ComputerName $server -Count 1 -Quiet\n        # If the machine is online\n        if($ping_result){\n        # Using \"Get-WmiObject Win32_OperatingSystem\" to get $OS object\n        # Using \"Get-WmiObject Win32_OperatingSystem -ComputerName $Testcomputer -ErrorAction Stop\" to get $OS object on remote machines.\n        $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $server -ErrorAction Stop\n        # Get LastReboot via $OS.LastBootUpTime variable\n        $LastReboot =($OS.ConvertToDateTime($OS.LastBootUpTime)).tostring(\"dd-MM-yyyy\")    \n        # Get Uptime via $OS.LastBootUpTime variable\n        $Uptime = (Get-Date) -$OS.ConvertToDateTime($OS.LastBootUpTime)\n        # Create a hash table (dictionary type) with 3 columns: PSComputerName, LastReboot and Uptime\n        $lastlogonproperties = @{\n        # Add PSComputerName into column #1\n        PSComputerName  = $server\n        # Convert and only get dd-mm-yyyy on last reboot variable.\n        # Add LastReboot into column #2\n        # Only get dd-mm-yyy field on LastReboot record.\n        LastReboot =($OS.ConvertToDateTime($OS.LastBootUpTime)).tostring(\"dd-MM-yyyy\")\n        # Add LastReboot into column #3\n        Uptime  = ([String]$Uptime.Days + \" Days \" + $Uptime.Hours + \" Hours \" + $Uptime.Minutes + \" Minutes\")\n        }\n\n    # Create a new table object on PS to append the hash table values above   \n    $forcecsv = New-Object psobject -Property $lastlogonproperties\n    # change order of columns before appending to csv file. \n    # Convert it to csv file and save under LastReboot directory.\n    $forcecsv | select-object PSComputerName, LastReboot, Uptime | export-csv -NoTypeInformation -append \"C:\\Shared\\LastReboot\\Server\\$filename.csv\"\n    }\n}<\/code><\/pre>\n\n\n\n<p>Run the script.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"86\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-243.png\" alt=\"\" class=\"wp-image-1678\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-243.png 442w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-243-300x58.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-243-200x39.png 200w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"674\" height=\"281\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-244.png\" alt=\"\" class=\"wp-image-1679\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-244.png 674w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-244-300x125.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-244-200x83.png 200w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><figcaption>Then, do the same PS script to query Windows 10 machines.<\/figcaption><\/figure>\n\n\n\n<p>Change &#8220;(operatingsystem -like &#8220;*Windows Server*&#8221;)&#8221; to (operatingsystem -like &#8220;*Windows 10*&#8221;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$computers = (Get-ADComputer -properties OperatingSystem -filter{(operatingsystem -like \"*Windows 10*\")}).name<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\"># This script is written by Tung on 2022-02-18\n# This is is used t get lastreboot and uptime on Windows 10 machines.\n# Get time when running the script\n$filename = (Get-Date).tostring(\"dd-MM-yyyy-hh-mm\")\n# Change PowerShell working directory to C:\\Shared\nSet-Location C:\\Shared\n#$TestComputerName = get-content C:\\Shared\\tungmachine.txt\n$computers = (Get-ADComputer -properties OperatingSystem -filter{(operatingsystem -like \"*Windows 10*\")}).name\nForeach ($Computer in $computers) {\n        # Only check the machine if it is online\n        $ping_result = Test-Connection -ComputerName $Computer -Count 1 -Quiet\n        # If the machine is online\n        if($ping_result){\n        # Using \"Get-WmiObject Win32_OperatingSystem\" to get $OS object\n        # Using \"Get-WmiObject Win32_OperatingSystem -ComputerName $Testcomputer -ErrorAction Stop\" to get $OS object on remote machines.\n        $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop\n        # Get LastReboot via $OS.LastBootUpTime variable\n        $LastReboot =($OS.ConvertToDateTime($OS.LastBootUpTime)).tostring(\"dd-MM-yyyy\")    \n        # Get Uptime via $OS.LastBootUpTime variable\n        $Uptime = (Get-Date) -$OS.ConvertToDateTime($OS.LastBootUpTime)\n        # Create a hash table (dictionary type) with 3 columns: PSComputerName, LastReboot and Uptime\n        $lastlogonproperties = @{\n        # Add PSComputerName into column #1\n        PSComputerName  = $Computer\n        # Convert and only get dd-mm-yyyy on last reboot variable.\n        # Add LastReboot into column #2\n        # Only get dd-mm-yyy field on LastReboot record.\n        LastReboot =($OS.ConvertToDateTime($OS.LastBootUpTime)).tostring(\"dd-MM-yyyy\")\n        # Add LastReboot into column #3\n        Uptime  = ([String]$Uptime.Days + \" Days \" + $Uptime.Hours + \" Hours \" + $Uptime.Minutes + \" Minutes\")\n        }\n\n    # Create a new table object on PS to append the hash table values above   \n    $forcecsv = New-Object psobject -Property $lastlogonproperties\n    # change order of columns before appending to csv file. \n    # Convert it to csv file and save under LastReboot directory.\n    $forcecsv | select-object PSComputerName, LastReboot, Uptime | export-csv -NoTypeInformation -append \"C:\\Shared\\LastReboot\\Client\\$filename.csv\"\n    }\n}<\/code><\/pre>\n\n\n\n<p>Run the script.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"555\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246-1024x555.png\" alt=\"\" class=\"wp-image-1681\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246-1024x555.png 1024w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246-300x163.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246-768x416.png 768w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246-200x108.png 200w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-246.png 1064w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"217\" src=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-247.png\" alt=\"\" class=\"wp-image-1682\" srcset=\"https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-247.png 684w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-247-300x95.png 300w, https:\/\/tungle.ca\/wp-content\/uploads\/2022\/02\/image-247-200x63.png 200w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this lab, I have used PS to get LastReboot and Uptime value on Windows servers and clients. PS script will query &#8220;Get-WmiObject&#8221; to get the last boot time of the computers\/servers. Firstly, I will test with Windows Servers. Write a full PS script. Run the script. Change &#8220;(operatingsystem -like &#8220;*Windows Server*&#8221;)&#8221; to (operatingsystem -like [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,7],"tags":[10,12],"class_list":["post-1675","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-powershell","tag-microsoft","tag-powershell"],"_links":{"self":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/1675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1675"}],"version-history":[{"count":4,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/1675\/revisions"}],"predecessor-version":[{"id":4318,"href":"https:\/\/tungle.ca\/index.php?rest_route=\/wp\/v2\/posts\/1675\/revisions\/4318"}],"wp:attachment":[{"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tungle.ca\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}