Skip to content

Instantly share code, notes, and snippets.

@tyranid
Created September 26, 2017 18:34
Show Gist options
  • Save tyranid/b3b7a37efcb361d599faca506b24f6c5 to your computer and use it in GitHub Desktop.
Save tyranid/b3b7a37efcb361d599faca506b24f6c5 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