File Cannot Be Accessed Fix with Handle.exe

File Cannot Be Accessed Fix with Handle.exe


Handle.exe is a 3rd party tool that can help track down a process that is locking down a file you are trying access. Here is how.Nothing is more annoying when you're trying to remove a file or folder on your Windows machine and get this error message:


This example gave us some helpful information like the name but there are many times when the information displayed is not quite so useful. When working on older versions of Windows, for example, there's no indication as to which process has locked the file.

To make things even more frustrating, there's not an easy way to track that process down via Windows without a third party tool! But, there is a way to track that process down with a utility called handle.exe and a little PowerShell.

Related Article: Working With File Catalogs in PowerShell 5.1
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.

First of all, you'll need to download and unzip handle. This can either be done manually, or if you're a PowerShell geek like me, you can do it via PowerShell.

Invoke-WebRequest -Uri '' -OutFile C:\
Expand-Archive -Path C:\

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. Let's change that with PowerShell. I'll write a small PowerShell function that will call handle.exe but gives me a chance to provide a single file to limit the output to.

function Find-LockedFileProcess {

        [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, 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.

New Call-to-action

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.

PS C:\> Find-LockedFileProcess -FileName TestWordDoc.docx

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.

Related Posts

Comments are disabled in preview mode.
Loading animation