PowerShell WMI SystemRestore

Introduction to PowerShell WMI SystemRestore SystemRestore WMI PowerShell

This real-life task for PowerShell is to control your desktop computer’s system restore points.

We will employ the WMI SystemRestore class to enable, or disable, the protection settings for restore points.

(Screenshot of Control Panel, System, Advanced Tab)

Topics for SystemRestore

 ♣

WMI Class SystemRestore

At the heart of this example is [WmiClass]"\\$SysName\root\default:systemrestore". Once we assign it to the variable $SysRestore, we can apply one of two methods, .Disable("C:\") or .Enable("C:\").

The rest of the script consists of two wrappers, one to switch between "Enable" and "Disable", the other wrapper is the function called Set-SysRestore.

Set-SysRestore Function

It is traditional to name PowerShell functions using existing verbs such as ‘get-‘ or ‘set-‘.  At the heart of this function is the switch ($RestoreOpt) which calls upon the WMI class systemrestore.  It is this parameter which provides the option to enable or disable the system restore.

One important point, you need to supply a different value for $sysName.

##########################################
##
## Author: Dave Stone, Brooks Automation
## Date: 6 May 2010
## Enables or disables the system restore on a given computer
##
## Usage Set-SysRestore option computername
##
######################################

Function Set-SysRestore {
param(
$RestoreOpt = $(throw "please specify option, disable or enable"),
$sysName = $(throw "please specify a computer name or IP Address. `
Enclose entries containing spaces with single quotes")
)
switch ($RestoreOpt)
{
   "disable"
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
    $SysRestore.Disable("C:\")}
   "enable"
   {$SysRestore = [wmiclass]"\\$sysname\root\default:systemrestore"
    $SysRestore.Enable("C:\")}
   }
                             }
Set-SysRestore -RestoreOpt disable -SysName 192.168.1.166

Note 0:  This is one of those PowerShell scripts where you need to ‘Run as Administrator’.  Also Restore Points are a feature of client operating systems such as Windows 7 or Vista, and not servers. SystemRestore WMI PowerShell Function Parameters

Note 1:  Trace the two options for the -RestoreOpt parameter.  The last line puts the function Set-SysRestore to work, and disables the system restore on the C:\.

Note 2:  To turn on your restore, issue the reverse command:
Set-SysRestore -RestoreOpt enable -SysName YourComputer

Example:
Set-SysRestore -RestoreOpt enable -SysName MyComputer

Note 3: Pay close attention to -SysName, your Windows 7 or Vista machine is unlikely to have an IP address of 192.168.1.166.  You could of course use the hostname.

 

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Possible Reasons for Turning Off System Restore

When I was trying to repair a Vista machine, I got into a knot trying a system restore; this was because the anti-virus program kept giving a false positive reaction to a file that I was trying to repair.  By creating a PowerShell function I was able to control system restore while I sorted out the anti-virus problem.

Maybe the drive has run out of free space and you need to stop system restore consuming any more disk space.

Disable-ComputerRestore

One Advantage of the above WMI technique is that you can turn off system protection on the D, E or other drives.  However, if you just wish to disable system restore on the C:\ drive then you can use PowerShell v 2.0’s Disable-ComputerRestore cmdlet.

# PowerShell 2.0 Disable ComputerRestore Script
Clear-Host
# Enable-ComputerRestore -drive "C:\"
Disable-ComputerRestore -drive "C:\"

Kiwe CatTools - Free downloadGuy’s Challenge – Download this free device backup utility

(CatTools)

Kiwi CatTools is a free program for backing up configuration settings on hardware devices.  Here is Guy’s challenge.  If you download CatTools, then it will not only take care of backups, but also it will show you something new about the hardware on you network. I could give you a money back guarantee – but CatTools is already free!  Thus, I just make a techie to techie challenge, you will learn more about your network if you:

Download your free Kiwi CatTools configuration backup tools

Enable-ComputerRestore

Let us assume that you fixed the anti-virus program, or the low disk space problem that made it desirable to turn off creating restore points.  It’s a straightforeward task to reverse the above script by changing the Disable verb for Enable:

# PowerShell 2.0 Enable ComputerRestore Script
Clear-Host
Enable-ComputerRestore -drive "C:\"

Get-ComputerRestorePoint

In this scenario you wish to view a list of restore points, possibly with a view to recovering the computer to a former state.

# PowerShell 2.0 Get-ComputerRestorePoint
Clear-Host
Get-ComputerRestorePoint

PS F:\powershell\Text\SEO> Get-ComputerRestorePoint

CreationTime Description SequenceNumber EventType RestorePointType
———— ———– ————– ——— —————-
14/02/2012 13:21:31 Windows Update 232 BEGIN_SYSTEM_C… 18
15/02/2012 13:00:17 Windows Update 233 BEGIN_SYSTEM_C… 18
16/02/2012 13:00:12 Windows Update 234 BEGIN_SYSTEM_C… 18
17/02/2012 18:52:50 Installed SolarWinds Event … 235 BEGIN_SYSTEM_C… APPLICATION_INSTALL
19/02/2012 14:09:51 Windows Update 236 BEGIN_SYSTEM_C… 18
21/02/2012 13:00:12 Windows Update 237 BEGIN_SYSTEM_C… 18

See more on PowerShell restore computer »

Summary of PowerShell SystemRestore

Thanks to PowerShell’s [WMI] we can access SystemRestore and thus enable or disable protection of the C:\ or another drive.

If you like this page then please share it with your friends

 


See More Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • Win32_ComputerSystem   • Free WMI Monitor

WMI Class  • [WMI] Type  • Win32_printer   • Win32_product   • SystemRestore   • WMI Services

WMI Disk   • DNS   • Memory  • PowerShell -Filter   • Check Server UpTime   • ConvertToDateTime

Please email me if you have a script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.