Friday Fun: Find File Locking Process with PowerShell

I was asked on Twitter this morning about a way to find out what process has a lock on a given file. I’m not aware of any PowerShell cmdlet that can do that but I figured there had to be a .NET way and if I could find a code sample I could put something together in PowerShell. After some research I came to the conclusion that programmatic approaches were way above my pay grade but there is still an option I came up with.

I’m a big believer in the right tool for the job and just because you can do something in PowerShell, doesn’t mean you should. The best tool for this job is Handle.exe from the Sysinternals suite which you can download for free. You can specify part of a file name and it will show you the process that has a handle to that file.


But why not have your cake and eat it too? I can take this output and turn it into a PowerShell object. I posted a function recently to convert command line output to objects using regular expressions and named captures. All I need is a regular expression pattern.


Or you can create something specific to this task.

This requires Handle.exe. I’ve hardcoded the path which you need to adjust or make sure the utility is in your PATH. But now look what I can do!

I know some people get hung up on external dependencies but if it gets the job done I don’t see the issue. Especially when the solution is free to begin with!

Enjoy your weekend.

4 thoughts on “Friday Fun: Find File Locking Process with PowerShell

  1. My function assumes you have already run Handle.exe once before so you can accept the EULA. Otherwise, you could modify the function to always accept it.

    $data = &$handle /accepteula $path

  2. Pingback: Zajímavé novinky z IT z 11. týdne 2014 | Igorovo
  3. Had to make a few changes to get this working when multiple processes were returned by handles.exe.

    Also added commandline to the process. Not very elegant from a powershell perspective, I am still too c# minded however it works and is what I need. Thanks for your help.

  4. Pingback: Microsoft Most Valuable Professional (MVP) ā€“ Best Posts of the Week around Windows Server, Exchange, SystemCenter and more ā€“ #72 - Windows Management - TechCenter - Dell Community

Comments are closed.