Skip to content

Instantly share code, notes, and snippets.

@langheran
Created May 19, 2024 17:09
Show Gist options
  • Save langheran/2d4468da70251ea7180cf2ca100ef022 to your computer and use it in GitHub Desktop.
Save langheran/2d4468da70251ea7180cf2ca100ef022 to your computer and use it in GitHub Desktop.
C:\Users\NisimHurst\NDS\scripts\kill_drive.ps1
# Function to find and kill processes using the specified drive
function Kill-ProcessUsingDrive {
param (
[string]$DriveLetter
)
# Get the list of processes
$processes = Get-CimInstance -ClassName Win32_Process
# Filter processes that have a handle open on the specified drive
foreach ($process in $processes) {
$handlePaths = (Get-Process -Id $process.ProcessId -FileVersionInfo).FileName
foreach ($handle in $handlePaths) {
if ($handle -like "$DriveLetter*") {
try {
Write-Output "Killing process $($process.ProcessId) - $($process.Name)"
Stop-Process -Id $process.ProcessId -Force
} catch {
Write-Warning "Failed to kill process $($process.ProcessId): $_"
}
}
}
}
}
# Specify the drive letter
$driveLetter = "E:"
# Run the function
Kill-ProcessUsingDrive -DriveLetter $driveLetter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment