FixOffice.ps1
|
<#
.SYNOPSIS Fix Microsoft Office issues .DESCRIPTION This script stops Microsoft applications, removes Microsoft accounts and removes Office license keys before self-desctructing. #> if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { # If not running as administrator, re-launch the script with administrative privileges Start-Process -FilePath "powershell" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs exit } Read-Host -p "Press 'ENTER' to continue" function Backup { $date = Get-Date Checkpoint-Computer -Description $date -RestorePointType MODIFY_SETTINGS } Backup function endProcess { $processList = Get-Process | Select-Object -ExpandProperty ProcessName $processNameList = "msedge", "winword", "excel", "powerpnt", "outlook", "msaccess", "mspub", "onenote", "msteams", "lync", "visio" foreach ($process in $processNameList) { if ($processList -contains $process) { Stop-Process -Name $process } } } endProcesses function removeCreds { $credList = cmdkey /list | ForEach-Object { if (($_ -match "LegacyGeneric:target=MicrosoftAccount:user=(.*)") -xor ($_ -match "MicrosoftAccount:target=SSO_POP_User:user=(.*)")){ $matches[1] } } foreach ($targetName in $credList) { cmdkey /delete:"LegacyGeneric:target=MicrosoftAccount:user=$targetName" cmdkey /delete:"MicrosoftAccount:target=SSO_POP_User:user=$targetName" } } removeCredentials function removeKeys { $officeRegPath = "HKLM:\SOFTWARE\Microsoft\Office\8.0", "HKLM:\SOFTWARE\Microsoft\Office\10.0", "HKLM:\SOFTWARE\Microsoft\Office\12.0", "HKLM:\SOFTWARE\Microsoft\Office\15.0", "HKLM:\SOFTWARE\Microsoft\Office\16.0" foreach ($version in $officeRegPath) { if ($version) { Remove-Item -Path $version -Force -Recurse } } } removeKeys Write-Output "Process nearly complete, please sign into Word when the application opens." Start-Process winword $scriptPath = $MyInvocation.MyCommand.Path function selfDestruct { Start-Sleep -Seconds 5 Remove-Item -Path $scriptPath -Force } selfDestruct |