Skip to content

Instantly share code, notes, and snippets.

@clavoillotte
Forked from tyranid/kill_file_locker.ps1
Created March 10, 2019 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clavoillotte/1424594fb2a17753c36505663d671933 to your computer and use it in GitHub Desktop.
Save clavoillotte/1424594fb2a17753c36505663d671933 to your computer and use it in GitHub Desktop.
Import-Module NtObjectManager
<#
Function to kill all processes which are using a locked file.
#>
function Kill-FileLocker {
param(
[Parameter(Mandatory)]
[string]$Path
)
Set-NtTokenPrivilege SeDebugPrivilege | Out-Null
Use-NtObject($file = Get-NtFile $Path -Win32Path -Access ReadAttributes) {
foreach($id in $file.GetUsingProcessIds()) {
Use-NtObject($p = Get-NtProcess -ProcessId $id -Access Terminate) {
Write-Host "Killing process $p"
$p.Terminate(0)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment