How Can I Enable System Restore Using a Script?
Hey, Scripting Guy! How can I enable Windows XP’s System Restore using a script?
Hey, RL. Thanks for the question; the Scripting Guy who writes this column is running way late today, the clock is ticking, and he was desperately in need of a question a he could answer without having to use a lot of code. A question about enabling System Restore is just what the doctor ordered, or at least it would be if the doctor happened to be writing a daily scripting column. To tell you the truth, we don’t really know whether doctors write daily scripting columns or not. We’re guessing that most do, but that’s just a guess.
But here’s one thing we do know, for sure: contrary to popular belief, the international distress code SOS is not short for Save Our Ship. Instead, the letters S-O-S were chosen because they represent simple, all-but-impossible to misinterpret Morse code: three dots, three dashes, three dots. The letters don’t actually stand for anything at all.
Pardon? You’re suggesting that, if we really are way behind schedule (which we are), it might be better for us to talk about scripting rather than talk about some arcane trivia no one else cares about? You know, that’s good advice, and we’ll do it … assuming we ever stumble upon some arcane trivia that no one else cares about. But we hardly think the origin of SOS falls into that category.
On the other hand, now that we’ve pretty much exhausted our knowledge both of the SOS distress call and of Morse code, well, about all we can do is talk about the script. With that in mind, here’s a script that enables System Restore on a computer running Windows XP:
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default") Set objItem = objWMIService.Get("SystemRestore") errResults = objItem.Enable("")
You can see why, on a busy day, we chose to answer this particular question: just a few lines of code and we’ve got ourselves a column. The script itself starts out in typical fashion, connecting to the WMI service on the local computer. (Although, like all good WMI scripts, this one will also work against remote computers; just assign the name of that computer to the variable strComputer.) One thing to watch out for, however: unlike most WMI scripts this one doesn’t connect to the root\cimv2 namespace; instead, it connects to the root\default namespace. Why? That’s easy: that’s where all the System Restore WMI classes reside.
After making the connection we use the Get method to bind to the SystemRestore class; that’s what we do here:
Set objItem = objWMIService.Get("SystemRestore")
At that point we can enable System Restore on all drives by simply calling the Enable method and passing an empty string (“”) as the sole parameter.
Anyway, thanks again for the question, RL, we have to – well, yes, like we said, that does enable System Restore on all drives. Could we enable System Restore on a particular drive? Well, like we said we’re running kind of late here – oh, what the heck. Here’s a script that enables System Restore only for drive D on the local computer:
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default") Set objItem = objWMIService.Get("SystemRestore") errResults = objItem.Enable("D:\")
Yes, it’s remarkably similar to the first script we showed you; in fact, the only difference lies with the parameter passed to the Enable method. To enable System Restore on all drives we pass an empty string to this method; to enable System Restore only on drive D we pass the Enable method this parameter: D:\. In other words, we pass the drive letter (D) followed by :\. To enable System Restore on drive E and drive E only we’d use this line of code:
errResults = objItem.Enable("E:\")
We really have to go now, so – what’s that? What if you later want to disable System Restore on a computer? You know, we really don’t have time to …. Look, we’ll make you a deal: one more script and then we have to go. OK? OK. Here’s a script that disables System Restore for all the drives on a computer:
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default") Set objItem = objWMIService.Get("SystemRestore") errResults = objItem.Disable("")
Again, very similar to the first script we showed you; in fact, the only difference is that this time we call the Disable method instead of the Enable method. And, yes, as a matter of fact you can disable System Restore for a specific drive. Tired of running System Restore on drive D? Then don’t:
strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\default") Set objItem = objWMIService.Get("SystemRestore") errResults = objItem.Disable("D:\")
That’s all we have time for; we absolutely have to go. Although we probably should add that, prior to SOS, the international distress call was CQD. The letters CQ represented a general message that was sent to all stations, with the letter D indicating “distress.” In other words, CQD meant “Attention all stations, we have a distress call.” In fact, the ill-fated Titanic originally sent out several CQD calls before trying SOS. As it turns out – wait a second, where are you going? What do you mean you’re too busy to stay and listen to our story? Come on, surely you can make time for – this will only take a minute – are you sure you can’t stay just – oh. Well, OK. We can always save this story for a future column.