The Locked File Problem
How exactly does a file get locked?
During normal use, a process creates many handles to resources such as a file.
The
variable needs to be set to a full file path.
Julia Tim/Shutterstock
The downside is that this only works for files that are remotely accessed.
To close, you could pipe the open files returned to theClose-SMBOpenFilecommand.
At first glance, it looks perfect for your needs!
Open up an Administrative PowerShell prompt and trigger the commandopenfiles /query.
To turn this on, enter inopenfiles /local onand then reset your rig.
That being said, let’s see how we can make this work within PowerShell.
Using that information, you’re free to thendisconnecta file to unlock it.
Because of that, other solutions may be needed.
Conveniently, there is an utility namedhandlesthat provides exactly what you are looking for!
By doing so, you’re able to easily reference the utility wherever you need it.
But how do you make this easier to use?
The following code reads each process and retrieves just the locked files.
The downside is that this takes a while to run as there are many processes.
TMany possibilities exist, especially in combining the various methods into a solution that fits your environment.