SQL SERVER – Scripting Tip: PowerShell to Identify Last Bootup Time

I know I have been fortunate to write back-to-back on topics around SQL Server. I get a number of you do take time in writing down to me over emails, comments and even sometimes over Skype pings. I see every such mail as an opportunity to learn something from you for sure. One of my friends from the networking team pinged me to check if I knew a query about the last bootup time he had:

Admin: Hi buddy.

Pinal: Nice to hear from you. Been a while. How are you?

Admin: I know you have tons of script on SQL Server, I have a unique question. Can you help me?

Pinal: Not sure, how – take a shot please.

Admin: I want to know when the Server was started.

Pinal: That must be easy, you can look at the SQL Server Error Logs right? It will have the information.

Admin: Oh is it? Will it also have the time when Windows was started?

Pinal: Hold on !! You want to know about Windows Server?

Admin: Yes, that is exactly what I want to know.

Pinal: That is an interesting question. Let me give me a shot.

Admin: It would be of great help. Let me know.

Pinal: Sure, don’t worry – it is going to feature in the blog soon.

Admin: Well, that will be of great help to all.

Pinal: Welcome. Not an issue.

This blog was inspired by this conversation I had. I went about searching various options, but I wanted to give it a shot using PowerShell. There are a number of powershell scripts blogged before but this was a great challenge.

I made a simple script below that made my day too:

#Show the important details of Win32_OperatingSystem
$computer = "LocalHost"
$namespace = "root\CIMV2"
Get-WmiObject -class Win32_OperatingSystem -computername $computer -namespace $namespace
| Select OSArchitecture, BuildNumber, Caption, LastBootUpTime | FT

The output for the same looks like:

SQL SERVER - Scripting Tip: PowerShell to Identify Last Bootup Time Windows-Started-01

As you can see, I have got some extra info about the OS too in this script. But it gave me some very interesting information around the last bootup time. This is exactly what my friend was searching and I did send him the same via email. He was pleasantly surprised and loved the simplicity in the solution.

How have you ever used some PowerShell scripts in your environments? Do let me know via comments.

Reference: Pinal Dave (https://blog.sqlauthority.com)

, ,
Previous Post
Interview Question of the Week #059 – What are the Limitations of User Defined Functions (UDF) ?
Next Post
SQL SERVER – How to Start SQL Server Service Without tempdb?

Related Posts

2 Comments. Leave new

  • Hi Pinal,
    Great one!

    I found other options for this 1) Task Manager – > Performance -> uptime 2) CMD -> Net stats srv.

    Reply
  • Chalapathi Pinisetty
    February 25, 2016 9:24 am

    Hi Pinal,

    I am using a simple command from command prompt to verify last boot time. Just type below command in command prompt.

    systeminfo | find /i “Boot Time”

    Regards

    Reply

Leave a Reply

Menu