Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.