File in use / Use PowerShell to find out witch process is locking a file

When we tried to upgrade to the latest Nextcloud Client (Version2.6.5), we were confronted with the following error message.

The file C:\Program Files\Nextcloud\shellext\OCOverlays.dll is in-use and can’t be overwritten -> (if you think about it, it’s already clear that the Explorer Proess ‘locked’ this file, as this library is most likely used to overlay the icons … )

Now there are command line tools like Handles.exe from SysInternals or Openfiles and many other GUI applications that help you to find out which process is locking a file. But in the age of PowerShell and the possibilities it offers it seems to me to be overkill to install a tool for this purpose.

With PowerShell you can find out with the following command, which process locks a file.

$lockedfile = 'C:\Program Files\Nextcloud\shellext\OCOverlays.dll'
Get-Process | foreach{$processVar = $_;$_.Modules | foreach{if($_.FileName -eq $lockedFile){$processVar.Name + " PID:" + $}}}