# Define log file location $logFile = "C:\PathUpdateLog.txt" # Retrieve services that have spaces and are missing quotes $services = Get-WmiObject Win32_Service | Where-Object { $_.PathName -match '^[^"].*\s.*[^"]$' -and $_.PathName -notmatch '^C:\\WINDOWS\\System32\\' -and $_.PathName -notmatch 'C:\\WINDOWS\\CCM\\TSManager.exe' -and $_.PathName -notmatch '^C:\\WINDOWS\\Microsoft.NET\\' } | Select-Object DisplayName, PathName, Name # Start logging Add-Content -Path $logFile -Value "=== Path Update Log - $(Get-Date) ===" foreach ($service in $services) { $originalPath = $service.PathName # Extract the executable portion if ($originalPath -match '^(.*\.exe)\s+(.*)$') { $exePath = $matches[1] # Extracts the .exe portion $arguments = $matches[2] # Extracts any arguments $quotedPath = "`"$exePath`" $arguments" } else { $quotedPath = "`"$originalPath`"" # If no arguments, just quote the full path } # Log before change Add-Content -Path $logFile -Value "Updating: $($service.DisplayName)" Add-Content -Path $logFile -Value "Before: $originalPath" # Update the registry Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\$($service.Name)" -Name ImagePath -Value $quotedPath # Log after change Add-Content -Path $logFile -Value "After: $quotedPath" Add-Content -Path $logFile -Value "---------------------------------------------" Write-Output "Updated: $($service.DisplayName) -> $quotedPath" } Write-Output "Log saved to $logFile"