Created
March 28, 2023 02:58
-
-
Save CRTX/39b168473e97590b42d5af3127501c5e to your computer and use it in GitHub Desktop.
Battlefield2042 Cleaner
This file contains 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
# Check if the script is running with administrator privileges | |
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
# If the script is not running with administrator privileges, prompt the user to elevate | |
if (-not $isAdmin) { | |
Write-Host "This script requires administrator privileges. Please enter your credentials to continue:" | |
Start-Process powershell.exe -Verb RunAs -ArgumentList "-File `"$($MyInvocation.MyCommand.Path)`"" | |
exit | |
} | |
# If the script is running with administrator privileges, continue executing the script | |
Write-Host "Running with administrator privileges." | |
# Get a list of processes with the name EADesktop.exe | |
$processes = Get-Process | Where-Object { $_.Name -eq "EADesktop.exe" } | |
# Close each process gracefully | |
foreach ($process in $processes) { | |
Write-Host "Closing process $($process.Name) (PID $($process.Id))..." | |
$process.CloseMainWindow() | |
} | |
# Wait for all EADesktop.exe processes to close | |
while ($processes.HasExited -contains $false) { | |
Write-Host "Waiting for EADesktop.exe processes to close..." | |
Start-Sleep -Seconds 1 | |
} | |
Write-Host "All EADesktop.exe processes have closed. Started cleaning..." | |
# Define the path to the Battlefield 2042 folder | |
$battlefieldPath = Join-Path $env:userprofile "documents\Battlefield 2042" | |
# Define the path to the Screenshots folder | |
$screenshotsPath = Join-Path $battlefieldPath "Screenshots" | |
# Get a list of files and folders in the Battlefield 2042 folder, excluding the Screenshots folder | |
$itemsToDelete = Get-ChildItem $battlefieldPath | Where-Object { $_.FullName -ne $screenshotsPath } | |
# Delete each item in the list | |
foreach ($item in $itemsToDelete) { | |
if ($item.PSIsContainer) { | |
# If the item is a folder, delete it and all its contents recursively | |
Write-Host "Deleting folder $($item.FullName)..." | |
Remove-Item $item.FullName -Recurse -Force | |
} | |
else { | |
# If the item is a file, delete it | |
Write-Host "Deleting file $($item.FullName)..." | |
Remove-Item $item.FullName -Force | |
} | |
} | |
Write-Host "Finished deleting files and folders in $battlefieldPath, except for the Screenshots folder." | |
#Stop EA Service or it won't delete everything in the next folder | |
Write-Host "Stopping EABackgroundService..." | |
Stop-Service -Name EABackgroundService | |
# Delete everything inside the EA Desktop folder in ProgramData | |
Write-Host "Deleting everything inside ProgramData\EA Desktop" | |
Remove-Item -Path "$env:ProgramData\EA Desktop\*" -Recurse -Force | |
# Restart the computer | |
Restart-Computer | |
# Write a message to the console | |
Write-Host "Press any key to exit..." | |
# Wait for the user to press a key | |
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment