2026-07-19 22:06:07 +02:00
function Invoke-SystemRestorePoint {
2026-03-07 20:28:48 +01:00
$failed = $false
2026-08-15 17:08:00 +02:00
$isSilent = ( $script:Params -and $script:Params . ContainsKey ( 'Silent' )) -or $script:Silent
2026-03-07 20:28:48 +01:00
2026-08-15 17:08:00 +02:00
try {
$SysRestore = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SystemRestore" -Name "RPSessionInterval" -ErrorAction Stop
}
catch {
Write-Host "Error: Unable to determine whether System Restore is enabled: $( $_ . Exception . Message ) " -ForegroundColor Red
$failed = $true
}
if ( -not $failed -and $SysRestore . RPSessionInterval -eq 0 ) {
2026-03-07 20:28:48 +01:00
# In GUI mode, skip the prompt and just try to enable it
2026-08-15 17:08:00 +02:00
if ( $script:GuiWindow -or $isSilent -or $ ( Read-Host -Prompt "System restore is disabled, would you like to enable it and create a restore point? (y/n)" ) -eq 'y' ) {
2026-03-15 22:58:06 +01:00
try {
2026-05-17 18:50:36 +03:00
$enableResult = Invoke-NonBlocking -TimeoutSeconds 90 -ScriptBlock {
2026-03-15 22:58:06 +01:00
try {
Enable-ComputerRestore -Drive " $env:SystemDrive "
return $null
}
catch {
return "Error: Failed to enable System Restore: $_ "
}
2026-03-07 20:28:48 +01:00
}
2026-03-15 22:58:06 +01:00
}
catch {
$enableResult = "Error: Failed to enable System Restore: $_ "
2026-03-07 20:28:48 +01:00
}
2026-03-15 22:58:06 +01:00
if ( $enableResult ) {
Write-Host $enableResult -ForegroundColor Red
2026-03-07 20:28:48 +01:00
$failed = $true
}
}
else {
$failed = $true
}
}
if ( -not $failed ) {
2026-03-15 22:58:06 +01:00
try {
2026-05-17 18:50:36 +03:00
$result = Invoke-NonBlocking -TimeoutSeconds 90 -ScriptBlock {
2026-03-07 20:28:48 +01:00
try {
2026-03-15 22:58:06 +01:00
$recentRestorePoints = Get-ComputerRestorePoint | Where-Object { ( Get-Date ) - [ System.Management.ManagementDateTimeConverter ]:: ToDateTime ( $_ . CreationTime ) -le ( New-TimeSpan -Hours 24 ) }
2026-03-07 20:28:48 +01:00
}
catch {
2026-03-15 22:58:06 +01:00
return [ PSCustomObject ] @ { Success = $false ; Message = "Error: Unable to retrieve existing restore points: $_ " }
}
if ( $recentRestorePoints . Count -eq 0 ) {
try {
Checkpoint-Computer -Description "Restore point created by Win11Debloat" -RestorePointType "MODIFY_SETTINGS"
return [ PSCustomObject ] @ { Success = $true ; Message = "System restore point created successfully" }
}
catch {
return [ PSCustomObject ] @ { Success = $false ; Message = "Error: Unable to create restore point: $_ " }
}
}
else {
return [ PSCustomObject ] @ { Success = $true ; Message = "A recent restore point already exists, no new restore point was created" }
2026-03-07 20:28:48 +01:00
}
}
2026-03-15 22:58:06 +01:00
}
catch {
$result = [ PSCustomObject ] @ { Success = $false ; Message = "Error: Failed to create system restore point: $_ " }
2026-03-07 20:28:48 +01:00
}
2026-03-15 22:58:06 +01:00
if ( $result -and $result . Success ) {
Write-Host $result . Message
}
elseif ( $result ) {
Write-Host $result . Message -ForegroundColor Red
2026-03-07 20:28:48 +01:00
$failed = $true
}
else {
2026-03-15 22:58:06 +01:00
Write-Host "Error: Failed to create system restore point" -ForegroundColor Red
$failed = $true
2026-03-07 20:28:48 +01:00
}
}
# Ensure that the user is aware if creating a restore point failed, and give them the option to continue without a restore point or cancel the script
if ( $failed ) {
if ( $script:GuiWindow ) {
$result = Show-MessageBox "Failed to create a system restore point. Do you want to continue without a restore point?" "Restore Point Creation Failed" "YesNo" "Warning"
if ( $result -ne "Yes" ) {
$script:CancelRequested = $true
2026-08-15 17:08:00 +02:00
return $false
2026-03-07 20:28:48 +01:00
}
}
2026-08-15 17:08:00 +02:00
elseif ( -not $isSilent ) {
2026-03-07 20:28:48 +01:00
Write-Host "Failed to create a system restore point. Do you want to continue without a restore point? (y/n)" -ForegroundColor Yellow
if ( $ ( Read-Host ) -ne 'y' ) {
$script:CancelRequested = $true
2026-08-15 17:08:00 +02:00
return $false
2026-03-07 20:28:48 +01:00
}
}
Write-Host "Warning: Continuing without restore point" -ForegroundColor Yellow
2026-08-15 17:08:00 +02:00
return $false
2026-03-07 20:28:48 +01:00
}
2026-08-15 17:08:00 +02:00
return $true
2026-05-17 18:50:36 +03:00
}