<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1678611822423757&amp;ev=PageView&amp;noscript=1">
Defrag This

| Read. Reflect. Reboot.

File Cannot Be Accessed Fix with Handle.exe

Adam Bertram| March 07 2017

| IT insights

file-cannot-be-accessed-fix-handle-exe.jpgHandle.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 'https://download.sysinternals.com/files/Handle.zip' -OutFile C:\handle.zip
Expand-Archive -Path C:\handle.zip

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.

Topics: IT insights

Leave a Reply

Your email address will not be published. Required fields are marked *


Adam Bertram is a 20-year veteran of IT. He’s currently an automation engineer, blogger, independent consultant, freelance writer, author, and trainer. Adam focuses on DevOps, system management, and automation technologies as well as various cloud platforms. He is a Microsoft Cloud and Datacenter Management MVP and efficiency nerd that enjoys teaching others a better way to leverage automation.

Free Trials

Getting started has never been easier. Download a trial today.

Download Free Trials

Contact Us

Let us know how we can help you. Focus on what matters. 

Send us a note

Subscribe to our Blog

Let’s stay in touch! Register to receive our blog updates.