How to Write a PowerShell Script to Check Windows Up Time

How to Write a PowerShell Script to Check Windows Up Time
Have you ever wondered how to check boot time or how long a server has been up? In Linux, we can do this by using the command uptime.

[] [~] # uptime
 11:40:13 up 946 days,  4:57,  1 user,  load average: 0.03, 0.20, 0.15
[] [~] #

 What about on Windows Server?

 We will develop a PowerShell script, save it to a file called “CheckUpTime.ps1″ and we can use it to check boot time for any Windows server on the network. Ready?

 Open NotePad and write the following text:

 Param([Parameter (Mandatory=$true)] [string]$ServerName)

 Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ServerName | Select-Object -Property CSName, @{n="Last Boot Time"; 


Now save the file with a PS1 extension. I saved mine in the root of my D: drive.

To execute the script, make sure you change directory to where your script is:
Enter the following:

.\CheckUpTime.ps1 -ServerName localhost

If you get an error like this while executing the script:

It means that you need to Set-ExecutionPolicy to RemoteSigned. Now Type: Set-ExecutionPolicy RemoteSigned and enter Y to confirm, then try to execute the script again.

That’s it! Now you have a script which you can save it to a USB drive for your administration purposes.