---
title: "Unlock File Locks: Sysinternals Handle & PowerShell"
description: "Discover what process has a file locked. Learn to use Sysinternals Handle with PowerShell."
canonical: "https://adamtheautomator.com/sysinternals-handle/"
---

# Unlock File Locks: Sysinternals Handle & PowerShell

> Discover what process has a file locked. Learn to use Sysinternals Handle with PowerShell.

Source: https://adamtheautomator.com/sysinternals-handle/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Unlock File Locks: Sysinternals Handle & PowerShell](https://adamtheautomator.com/wp-content/uploads/2019/07/file-cannot-accessed-fix-handle-exe-denied.png)

# Unlock File Locks: Sysinternals Handle & PowerShell

[![](https://secure.gravatar.com/avatar/d0b9d42e21e5622713f8b693aa5c0f9244d5f7dd200ed29b8398f52dee5de337?s=192&d=mm&r=g)Adam Bertram](https://adamtheautomator.com/author/adam-bertram/)1 July 20192 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[File Management](/tag/file-management/)[PowerShell](/tag/powershell/)

Table of Contents

*   [Downloading Handle](#h-downloading-handle)
*   [Running Handle](#h-running-handle)

Sysinternal’s _handle.exe_ is a third-party tool that can help track down a process that is locking down a file you are trying to access. In this article, you’re going to learn how to download the handle utility and even build a small PowerShell script to automate the process of finding locked files!

The best way to track down processes that have your files open is the third party utility handle.exe. Part of the popular SysInternals tool set, _handle.exe_ looks at the file system and attempts to find all open file handles.

As part of its output, it also returns the process. We can use some PowerShell to wrap some code around this utility to provide an easy way to provide handle.exe with a particular file path and then be presented with a process.

## Downloading Handle

First of all, you’ll need to [download and unzip handle](https://technet.microsoft.com/en-us/sysinternals/bb896655.aspx). This can either be done manually, or if you’re a PowerShell geek like me, you can do it via PowerShell.

```powershell
Invoke-WebRequest -Uri 'https://download.sysinternals.com/files/Handle.zip' -OutFile C:\handle.zip
PS> Expand-Archive -Path C:\handle.zip
```

## Running Handle

Once downloaded, let’s now just run handle and see what the output looks like. You can see that the output is pretty ugly and there’s no way to limit it down by a file name.

![Running the SysInternals handle utility](https://adamtheautomator.com/wp-content/uploads/2020/12/image-1024x499.png)

Running the SysInternals handle utility

With the standard output alone, it will be a pain to track down a locked file. To fix that, you can create a little [PowerShell function](https://adamtheautomator.com/powershell-functions/ "PowerShell function") to parse the output and find only the line you’re interested in.

```powershell
function Find-LockedFileProcess {
     param(
         [Parameter(Mandatory)]
         [string]$FileName,
 
         [Parameter()]
         [string]$HandleFilePath = 'C:\Handle\handle.exe'
     )
 
     $splitter = '------------------------------------------------------------------------------'
     $handleProcess = ((& $HandleFilePath) -join "`n") -split $splitter | Where-Object {$_ -match [regex]::Escape($FileName) }
     (($handleProcess -split "`n")[2] -split ' ')[0]
 }
```

This small PowerShell function is doing a lot of text parsing. Unfortunately, the Sysinternals handle.exe is a command-line utility. When it was created, it had no idea what PowerShell was so going in line with other utilities of the time just dumped a bunch of text to the console.

As you can see, we can make it more user-friendly, but it requires some ugly string parsing like using the split operator, the new line character (\`n) and some regular expressions. Regardless, it only has to be done once, and I’ve already done it for you!

Let’s now copy and paste this function into a PowerShell console and give it a spin. I’ve opened up a Microsoft Word document called TestWordDoc.docx. I know that Word does lock the file when open. Let’s see if our function can find what process is locking that file.

```powershell
PS> Find-LockedFileProcess -FileName TestWordDoc.docx
WINWORD.EXE
```

Success! You can see now instead of seeing all of that output at once; we can provide a single file name and get back a single process. This is exactly what we’re after.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fsysinternals-handle%2F&text=Unlock%20File%20Locks%3A%20Sysinternals%20Handle%20%26%20PowerShell)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fsysinternals-handle%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fsysinternals-handle%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2021/08/Icacls-The-Ultimate-Guide.jpg)

### [Icacls: The Ultimate Guide in Managing File Permissions](/icacls/)

Learn how to grant, deny, remove permissions, and more with the icacls command in this ultimate guide!

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea65f.jpg)

### [Automate File Transfers with PowerShell & Scheduled Tasks](/automated-file-transfer/)

Set up automated file transfers using PowerShell and create scheduled tasks to run your script on a recurring basis.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/5d238ae5c824b514689ea638.jpg)

### [Temp File Cleaner: A PowerShell Script Guide](/temp-file-cleaner/)

Reclaim your storage space. Learn to build a custom temp file cleaner with PowerShell.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
