Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Friday Fun: Find File Locking Process with PowerShell

Posted on March 14, 2014

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
ManageEngine ADManager Plus - Download Free Trial

Exclusive offer on ADManager Plus for US and UK regions. Claim now!

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.

handle

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.

PS Scripts:\>[regex]$matchPattern = "(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\b(\d+)\b)\s+type:\s+(?<Type>\w+)\s+\w+:\s+(?<Path>.*)"
PS Scripts:\> g:\sysinternals\handle parent.lock | select -skip 5 | convertfrom-text $matchpattern

converthandle

Or you can create something specific to this task.

Function Get-LockingProcess {

[cmdletbinding()]
Param(
 [Parameter(Position=0,Mandatory=$True,
 HelpMessage="What is the path or filename? You can enter a partial name without wildcards")]
 [Alias("name")]
 [ValidateNotNullorEmpty()]
 [string]$Path
)

#define the path to Handle.exe
$Handle = "G:\Sysinternals\handle.exe"

[regex]$matchPattern = "(?<Name>\w+\.\w+)\s+pid:\s+(?<PID>\b(\d+)\b)\s+type:\s+(?<Type>\w+)\s+\w+:\s+(?<Path>.*)"

$data = &$handle $path 
$MyMatches = $matchPattern.Matches( $data )

if ($MyMatches.value) {
      $MyMatches | foreach {
     [pscustomobject]@{ 
      FullName = $_.groups["Name"].value
      Name = $_.groups["Name"].value.split(".")[0]
      ID = $_.groups["PID"].value
      Type = $_.groups["Type"].value
      Path = $_.groups["Path"].value
     } #hashtable
    } #foreach
  } #if data
else {
    Write-Warning "No matching handles found"
  }
} #end function

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!
getlockingprocess

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

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

  1. Jeffery Hicks says:
    March 14, 2014 at 2:27 pm

    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. Carl Reid says:
    March 17, 2014 at 9:04 am

    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.

    http://pastebin.com/sZ90WP7H

  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.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d