Hello.Build definition in Python which list system restore points in Windows 11. List…
- User
Hello.Build definition in Python which list system restore points in Windows 11. List them and choose to restore,then restore. In pywin32,pywin,or other windows API in Python.
Assistant
Windows System Restore Points Management in Python
To list and restore system restore points in Windows 11 using Python, you can utilize the Windows Management Instrumentation (WMI) interface through the
wmimodule. Here's a complete solution:import wmi import subprocess import ctypes import sys from datetime import datetime def is_admin(): """Check if the script is running with administrator privileges""" try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False def list_restore_points(): """List all available system restore points""" c = wmi.WMI() print("\nAvailable System Restore Points:") print("-" * 50) restore_points = c.Win32_ShadowCopy() if not restore_points: print("No restore points found.") return [] for i, rp in enumerate(restore_points, 1): try: install_date = datetime.strptime(rp.InstallDate.split('.')[0], '%Y%m%d%H%M%S') date_str = install_date.strftime('%Y-%m-%d %H:%M:%S') except: date_str = "Unknown date" print(f"{i}. ID: {rp.ID} | Created: {date_str} | Description: {rp.Description or 'No description'}") return restore_points def restore_to_point(restore_point_id): """Perform system restore to the specified restore point""" try: # Using rstrui.exe (System Restore UI) with command line parameters command = f'%windir%\\system32\\rstrui.exe /restorepoint:{restore_point_id}' subprocess.Popen(command, shell=True) print("\nSystem Restore wizard launched. Follow the on-screen instructions.") except Exception as e: print(f"Error launching System Restore: {e}") def main(): if not is_admin(): print("This script requires administrator privileges. Restarting with elevated permissions...") ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1) return print("Windows System Restore Point Manager") print("=" * 50) restore_points = list_restore_points() if not restore_points: return while True: try: choice = input("\nEnter the number of the restore point to use (or 'q' to quit): ") if choice.lower() == 'q': break index = int(choice) - 1 if 0 <= index < len(restore_points): selected_point = restore_points[index] print(f"\nYou selected: {selected_point.Description or 'No description'} (Created: {selected_point.InstallDate})") confirm = input("Are you sure you want to restore to this point? (y/n): ") if confirm.lower() == 'y': restore_to_point(selected_point.ID) break else: print("Restore cancelled.") else: print("Invalid selection. Please try again.") except ValueError: print("Please enter a valid number or 'q' to quit.") if __name__ == "__main__": main()Requirements
- Install the required packages:
pip install wmi pywin32
How It Works
-
Administrator Check: The script first checks if it's running with admin privileges (required for system restore).
-
Listing Restore Points: It uses WMI to query the
Win32_ShadowCopyclass to get all available restore points. -
User Selection: Presents the restore points to the user with creation dates and descriptions.
-
Restore Process: When a restore point is selected, it launches the Windows System Restore wizard (
rstrui.exe) with the selected restore point ID.
Important Notes
- This script must be run as administrator to work properly.
- The actual restore process is handled by Windows' built-in System Restore utility.
- System Restore will require a system reboot to complete the process.
- Always back up important data before performing a system restore.
Alternative Approach
If you prefer to use only
pywin32without thewmimodule, you can use this alternative to list restore points:import win32com.client def list_restore_points_win32com(): """List restore points using win32com""" wmi = win32com.client.GetObject("winmgmts:") restore_points = wmi.InstancesOf("Win32_ShadowCopy") for rp in restore_points: print(f"ID: {rp.ID} | Description: {rp.Description} | Created: {rp.InstallDate}") return restore_pointsThis provides the same functionality but uses a different WMI access method.
- Install the required packages:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.