<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>powershell command to get uptime Archives - the Sysadmin Channel</title>
	<atom:link href="https://thesysadminchannel.com/tag/powershell-command-to-get-uptime/feed/" rel="self" type="application/rss+xml" />
	<link>https://thesysadminchannel.com/tag/powershell-command-to-get-uptime/</link>
	<description>Documenting My Life as a System Administrator</description>
	<lastBuildDate>Tue, 08 Sep 2020 03:33:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">144174110</site>	<item>
		<title>Get Uptime and Last Reboot Status Using Powershell</title>
		<link>https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/</link>
					<comments>https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/#comments</comments>
		
		<dc:creator><![CDATA[Paul Contreras]]></dc:creator>
		<pubDate>Sun, 22 Jul 2018 06:36:52 +0000</pubDate>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Server Administration]]></category>
		<category><![CDATA[get uptime for servers]]></category>
		<category><![CDATA[get uptime powershell multiple servers]]></category>
		<category><![CDATA[Get-Uptime]]></category>
		<category><![CDATA[how to check last reboot time in windows server]]></category>
		<category><![CDATA[powershell command to get uptime]]></category>
		<category><![CDATA[powershell get last boot time of remote computers]]></category>
		<category><![CDATA[powershell get uptime of remote computer]]></category>
		<category><![CDATA[powershell get uptime one liner]]></category>
		<category><![CDATA[powershell last reboot]]></category>
		<category><![CDATA[powershell script to check server uptime]]></category>
		<category><![CDATA[powershell server uptime report]]></category>
		<guid isPermaLink="false">https://thesysadminchannel.com/?p=836</guid>

					<description><![CDATA[<p>If you&#8217;re on a single machine and want to check when the last time a computer rebooted, you would use the systeminfo command and search for the System Boot Time property of that command. But what if you wanted to&#8230; <a href="https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/" class="more-link">Continue Reading <span class="meta-nav">&#8594;</span></a></p>
<p>The post <a href="https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/">Get Uptime and Last Reboot Status Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you&#8217;re on a single machine and want to check when the last time a computer rebooted, you would use the <code>systeminfo</code> command and search for the <code>System Boot Time</code> property of that command.  But what if you wanted to check the status of multiple computers?  What about wanting to check them remotely?.  In this article I&#8217;ll share the Get-Uptime script that was initially written by Reddit user <a href="https://www.reddit.com/user/OttoVonMonstertruck" rel="noopener noreferrer" target="_blank">/u/OttoVonMonstertruck</a> and added upon by yours truly.  The whole idea is be to be able to get the last reboot time remotely as well as get-uptime for multiple computers in your org.</p>
<h2>Powershell Uptime Script &#8211; Why is it needed?</h2>
<p>There are many reasons for a systems administrator to want to know when the last time a computer or server was rebooted. One reason that comes to mind is how long it has gone without being patched.  Normally, at least in the Microsoft world, when you apply Windows Updates it requires a system restart for those installed updates to be applied.  So being able to run this one liner Powershell command would be able to tell you if it has been more than a month or not.</p>
<p>Aside from Windows updates,  I&#8217;m sure we have all come across that one user who absolutely hates to restart their computer.  They have like 50,000 tabs open along with 30 other windows open and wonder their system is always slow.  I&#8217;ve come across this several times during my previous life as a desktop administrator and it seems to always be an issue when they have to &#8220;stop what they&#8217;re doing and reboot.&#8221;  While there is some validity to it, there never seems to be a good time because of whatever reason they come up with.</p>
<p>I&#8217;m sure there are many more use cases to check the last reboot time in Windows Server but they can all be identified with this handy Powershell that I use pretty regularly.</p>
<h2>Get Uptime and Last Reboot Status &#8211; Powershell Script</h2>
<pre class="brush: powershell; title: ; notranslate">

Function Get-Uptime {
&lt;#
.Synopsis
    This will check how long the computer has been running and when was it last rebooted.
    For updated help and examples refer to -Online version.


.NOTES
    Name: Get-Uptime
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2018-Jun-16

.LINK
    https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/ -


.PARAMETER ComputerName
    By default it will check the local computer.


    .EXAMPLE
    Get-Uptime -ComputerName PAC-DC01, PAC-WIN1001

    Description:
    Check the computers PAC-DC01 and PAC-WIN1001 and see how long the systems have been running for.

#&gt;

    [CmdletBinding()]
    Param (
        [Parameter(
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            Position=0)]

        [string[]]
            $ComputerName = $env:COMPUTERNAME
    )

    BEGIN {}

    PROCESS {
        Foreach ($Computer in $ComputerName) {
            $Computer = $Computer.ToUpper()
            Try {
                $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
                $Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
                [PSCustomObject]@{
                    ComputerName  = $Computer
                    LastBoot      = $OS.ConvertToDateTime($OS.LastBootUpTime)
                    Uptime        = ([String]$Uptime.Days + &quot; Days &quot; + $Uptime.Hours + &quot; Hours &quot; + $Uptime.Minutes + &quot; Minutes&quot;)
                }

            } catch {
                [PSCustomObject]@{
                    ComputerName  = $Computer
                    LastBoot      = &quot;Unable to Connect&quot;
                    Uptime        = $_.Exception.Message.Split('.')[0]
                }

            } finally {
                $null = $OS
                $null = $Uptime
            }
        }
    }

    END {}

}

</pre>
<p>We&#8217;ll first want to start by getting an array of the computers we want to check against.  In my case I am going to run a Powershell command to get all the servers in my domain (that are not disabled) and save it to a variable. That portion looks like this:</p>
<pre class="brush: powershell; title: ; notranslate">

#Get all enabled servers that are in the domain.
$Servers = Get-ADComputer -Filter {(OperatingSystem -like &quot;*Server*&quot;) -and (Enabled -eq $true)} -Properties OperatingSystem | select -ExpandProperty Name | Sort-Object

</pre>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-All-Enabled-Servers-in-the-Domain.png" target="_blank" rel="noopener noreferrer"><img fetchpriority="high" decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-All-Enabled-Servers-in-the-Domain.png" alt="Get All Enabled Servers in the Domain" width="899" height="452" class="aligncenter size-full wp-image-841" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-All-Enabled-Servers-in-the-Domain.png 899w, https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-All-Enabled-Servers-in-the-Domain-768x386.png 768w" sizes="(max-width: 899px) 100vw, 899px" /></a><br />
&nbsp;</p>
<p>Once we got that we&#8217;ll run the <strong>Get-Uptime</strong> script:</p>
<pre class="brush: powershell; title: ; notranslate">

#Get all reboot times and put them in a variable to reference later.
$Reboots = Get-Uptime -ComputerName $Servers

#Sorting by Lastboot time.  
$Reboots | Sort-Object LastBoot

</pre>
<p>The results are sorted by LastBoot property and the output looks something like this.</p>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-Uptime-Script-Results.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-Uptime-Script-Results.png" alt="Get-Uptime Script Results" width="902" height="456" class="aligncenter size-full wp-image-843" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-Uptime-Script-Results.png 902w, https://thesysadminchannel.com/wp-content/uploads/2018/07/Get-Uptime-Script-Results-768x388.png 768w" sizes="(max-width: 902px) 100vw, 902px" /></a><br />
&nbsp;</p>
<h2>How to run the Get Uptime script</h2>
<p>In order to the run the script there are a couple of things you need to do.  First and foremost, you need to <a href="https://thesysadminchannel.com/set-execution-policy-in-powershell/" target="_blank" rel="noopener noreferrer">set your execution policy</a> to RemoteSigned.  This is a standard with running <em>any</em> Powershell script.</p>
<p>Next you need to dot source the script since it is a function.  To dot source the script do the following:</p>
<ul>
<li>Copy the script above and save it any location. In this example I&#8217;ll save it to my C:\_Scripts folder.</li>
<li>Within the Powershell Window type: <strong>. .\_Scripts\Get-Uptime.ps1 </strong>&#8211; Note the two dots before the backslash.</li>
</ul>
<p><a href="https://thesysadminchannel.com/wp-content/uploads/2018/07/Dot-Source-Get-Uptime-Script.png" target="_blank" rel="noopener noreferrer"><img decoding="async" src="https://thesysadminchannel.com/wp-content/uploads/2018/07/Dot-Source-Get-Uptime-Script.png" alt="Dot-Source-Get-Uptime-Script" width="901" height="454" class="aligncenter size-full wp-image-848" srcset="https://thesysadminchannel.com/wp-content/uploads/2018/07/Dot-Source-Get-Uptime-Script.png 901w, https://thesysadminchannel.com/wp-content/uploads/2018/07/Dot-Source-Get-Uptime-Script-768x387.png 768w" sizes="(max-width: 901px) 100vw, 901px" /></a><br />
&nbsp;</p>
<p>And that&#8217;s it for getting the Powershell last reboot status.  Hopefully this is exactly what you were looking for.  On a another note, be sure to check us out on our <a href="https://www.youtube.com/channel/UC9VnUjmZrNG3ithDZmG-S-g" rel="noopener noreferrer" target="_blank">YouTube Channel</a> for more awesome content.  I&#8217;m sure you&#8217;ll also find some of our real world Powershell scripts very useful here in our <a href="https://thesysadminchannel.com/powershell/" rel="noopener noreferrer" target="_blank">Powershell Gallery</a></p>
<p>The post <a href="https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/">Get Uptime and Last Reboot Status Using Powershell</a> appeared first on <a href="https://thesysadminchannel.com">the Sysadmin Channel</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://thesysadminchannel.com/get-uptime-last-reboot-status-multiple-computers-powershell/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">836</post-id>	</item>
	</channel>
</rss>
