1

is there a way to use the windows power shell to trace what application has a resource lock on a file?

Here is what i am looking for:

  • A command line or script to show the application that has a lock on a file.
  • A bonus to the above wold be detection of the user account that has the lock and how long the lock has persisted.

Follow up:

  • There is a power tool by SysInternals that will allow you to capture a full dump in the command shell of the above.
  • I found a script that appears to do the job as well (replace with the full path and file you want to see):

    PS> $handle = handle

    PS> foreach ($line in $handle) { if ($line -match '\S+\spid:') { $exe = $line } elseif ($line -match '') { "$exe - $line" } }

1 Answers1

1

Handle from Windows Sysinternals is a command line utility that can be used to determine what handles have a lock on a file. You can run handle from the command line with

handle c:\fileToCheck

Or you could write a PowerShell script to wrap the functionality of handle.

Sam Cogan
  • 38,736
  • 6
  • 78
  • 114