Last active
January 21, 2026 15:33
-
-
Save RezaBabapour/25c62088f57be887bba6302f3158f794 to your computer and use it in GitHub Desktop.
Local autoSave for Microsoft word exel powerPoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Run command: powershell -ExecutionPolicy Bypass -File .\autoSave.ps1 | |
| Add-Type -AssemblyName System.Windows.Forms | |
| Add-Type @" | |
| using System; | |
| using System.Runtime.InteropServices; | |
| public class WinActive { | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr GetForegroundWindow(); | |
| [DllImport("user32.dll")] | |
| public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); | |
| } | |
| "@ | |
| # === SETTINGS === | |
| $officeApps = @("WINWORD", "EXCEL", "POWERPNT") | |
| $logFile = "$PSScriptRoot\autosave_log.txt" | |
| function Write-Log { | |
| param($msg) | |
| $timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") | |
| "$timestamp - $msg" | Out-File -Append -FilePath $logFile -Encoding UTF8 | |
| } | |
| # Clear console and show initial message | |
| Clear-Host | |
| Write-Host "Office Autosave Script Started" -ForegroundColor Green | |
| Write-Host "Logging to: $logFile" -ForegroundColor Cyan | |
| Write-Host "Press Ctrl+C to stop" -ForegroundColor Yellow | |
| Write-Host "" | |
| Write-Log "=== Autosave script started ===" | |
| while ($true) { | |
| $timestamp = (Get-Date).ToString("HH:mm:ss") | |
| $hWnd = [WinActive]::GetForegroundWindow() | |
| if ($hWnd -ne 0) { | |
| $processId = 0 | |
| # Suppress the output | |
| $null = [WinActive]::GetWindowThreadProcessId($hWnd, [ref]$processId) | |
| if ($processId -gt 0) { | |
| $proc = Get-Process -Id $processId -ErrorAction SilentlyContinue | |
| if ($proc -and $officeApps -contains $proc.ProcessName) { | |
| [System.Windows.Forms.SendKeys]::SendWait("^s") | |
| $logMsg = "Autosave triggered for $($proc.ProcessName) (PID: $processId)" | |
| Write-Host "[$timestamp] $logMsg" -ForegroundColor Green | |
| Write-Log $logMsg | |
| } | |
| else { | |
| $procName = if ($proc) { $proc.ProcessName } else { "Unknown" } | |
| $logMsg = "Skipped - active window: $procName (PID: $processId)" | |
| Write-Host "[$timestamp] $logMsg" -ForegroundColor Gray | |
| Write-Log $logMsg | |
| } | |
| } | |
| else { | |
| $logMsg = "Skipped - could not get process ID" | |
| Write-Host "[$timestamp] $logMsg" -ForegroundColor Yellow | |
| Write-Log $logMsg | |
| } | |
| } | |
| else { | |
| $logMsg = "Skipped - no active window detected" | |
| Write-Host "[$timestamp] $logMsg" -ForegroundColor DarkGray | |
| Write-Log $logMsg | |
| } | |
| Start-Sleep -Seconds 60 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment