Created
May 19, 2024 17:09
-
-
Save langheran/2d4468da70251ea7180cf2ca100ef022 to your computer and use it in GitHub Desktop.
C:\Users\NisimHurst\NDS\scripts\kill_drive.ps1
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
# 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