Hi Everybody,
I am writing this post after a long gap between the articles due to the busy schedule that I am having and I have also been planning new things to get more information to you guys. So I want to start off by saying thank you to all you guys for being supportive and visiting the website.
So today we will see how to get the last boot time of remote computers using Powershell.
Let me tell you how I got the idea of writing this article, when I was working on an issue and I realised that the computer was not responding through the Remote Desktop and I was trying to restart the machine remotely, but was not sure if the commands were actually executing on the other end. So I decided to find what was the last time the computer was up which would give me some information.
Anyways moving on, let us see what we will be using to achieve this and how we can use this in our day to day activities if need arises.
There are two ways to get this information, first from Get-CimInstance which you can use if you are on Powershell 3.0 and above. The second way is using the WMI Objects if you are using Powershell 2.0.
Lets see the properties associated with both the cmdlets and which will help us get the desired output.
First let us see the Get-CimInstance using the Win32_OperatingSystem class. Type the below on the Powershell screen and look for the properties.
Get-CimInstance Win32_OperatingSystem | Get-Member
I am using this on the local machine, so if we have to check a remote computer we need to specify the Computername parameter. Let us see the help file now.
So the final command that we will be using as shown below:
Get-CimInstance -ClassName win32_OperatingSystem | select csname, lastbootuptime
The above will fetch the information related to the local computer and below on the remote computer.
Get-CimInstance -ClassName win32_operatingsystem -ComputerName ED-EX2K10-CAH | select csname, lastbootuptime
Now let us see how to get this information using Get-WMIObject.
If you notice, in the first example the output of LastBootupTime was showing a value which did not make sense to us. Hence, we have used ConverttoDateTime to make it human readable.
Now let us try combining both the machines and check.
You can append any number of machines as you saw here. If you have huge list, you can also make a text file and apply a foreach loop and get the information.
I hope this was informative and thank you for reading!
20 Comments
Hi Adil,
I see that CIM cmdlets use the WMI classes (like Windows32_OperatingSystem ), so what is the ddifference between WMI query and CIM query?
Hi Prateek,
CIM Classes are the parent classes on which WMI classes are built. To Manage the Windows infrastructure both CIM and WMI classes are necessary. CIM Classes got introduced in PowerShell 3.0, which leverages WS-Man protocol instead of native DCOM protocol used by WMI. You can google for more deeper information.
If its multiple servers how can I add server.txt file in first command.
You have to manually add the server names in the server.txt file one per line.
Dear Adil,
Can u please elaborate how can I use foreach loop for multiple system which names are stored in a file (e.g. c:\systems.txt).
Hi Rajat,
It should be pretty simple. Just assign the contents of the text file to a variable. Something like the below.
$Servers = Get-Content C:\systems.txt
foreach ($Server in $Servers){
Get-CimInstance -ClassName win32_operatingsystem -ComputerName $Server | select csname, lastbootuptime
}
Hope this helps.
Hi Adil. Just a comment to say ‘nice site’ and to keep the articles coming 🙂
All the best.
Thanks for the comment 🙂 Appreciate it 🙂
Really helpfull. just wanted to know the script for checking if we can RDP the server. as there are multiple servers to check on.
Hi Aditya,
Thanks for stopping by, I just came across this yesterday. This might be exactly what you are looking for.
http://www.bryanvine.com/2015/06/quick-script-test-pingrdp-check-if.html
Hi Adil,
How we can get the users activity logs like how many time they logged in etc in terminal server.
Hi Shoaib,
A quick google search got me this.
https://teusje.wordpress.com/2012/09/11/windows-server-logging-users-logon-and-logoff-via-powershell/
Hope this helps.
Hi,
I hope you can help me?
I have a CSV file with 2 columns, “ServerName” & “Description”. I want to do a foreach lookup on the server name and find the last reboot time for each server, however, I also want to output the “Description” column so it looks like this
ServerName Description LastRebootTime
SVWQQQQQ, Mailxtender 01/01/2016 08:00:00
SVWPPPPPP Mailxtender 01/01/2016 08:00:00
SVWRRRRRR Mailxtender 01/01/2016 08:00:00
Also, I want it to sort on last reboot time, if possible. Thanks
CSV FILE Contents
ServerName, Description
SVWQQQQQ,Mailxtender
SVWPPPPPP,Mailxtender
SVWRRRRRR,Mailxtender
SVWXXXXX,Enterprise Vault
SVWTTTTT,Enterprise Vault
SVWYYYYY,Enterprise Vault
SVWIIIIIIIIIIII,Enterprise Vault
Hi Raks,
Use the below.
Import-Csv C:\Servers.csv
$Output = foreach($Server in $Servers){
Get-CimInstance $Server.ServerName | select csname,lastBootupTime | Sort-Object -Property lastBootupTime
}
$Output | Export-Csv C:\Result.csv -NoTypeInformation
Unfortunately, you can have to manually add the description field to the new file.
Just a note. The script does not work for me. Something to do with the date conversion. If I just put the lastbootuptime I get the ugly thing (which is readable) but if I tack on convertodatetime it comes up blank. Not sure what the issue is.
it works only with local systems not remote
What’s not working?
Hi Adil nice and useful website
Hi Adil,
How we can get the boot time of machine (how much time taken for booting).
Thank you for posting this as it has the last bit I needed. Here is what I’m using:
Get-WmiObject win32_OperatingSystem -computer (Get-Content C:\SWSetup\wmiInventory.txt) | Select csname, @{label=’LastBootUTime’ ;expression={$_.ConvertToDateTime($_.LastBootUpTime)}} | Out-GridView