fuser equivalent in Powershell?

2.3k Views Asked by At

I want to delete a directory, but a process is using it.

 mv : Access to the path 'C:\Users\mike\Documents\myapp\node_modules\' is denied.

Explorer mentions the dir is in use. Is there a Windows Powershell equivalent of fuser?

Note this is a powershell question. I don't want to launch a GUI app.

2

There are 2 best solutions below

4
On BEST ANSWER

Try this one:

$lockedFolder="C:\Windows\System32"
Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id}}}

This looks for every process that's running in the folder. (or in subdirectories)

With this script, you'll get some more informations:

$lockedFolder="C:\Windows\System32" 
Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id + " FullName: " + $_.FileName }}}

I think there is no equivalent to fuser, but there is a tool called handle.exe that has to be installed first.

PowerShell script to check an application that's locking a file?

0
On

Here's a modified version of @Eldo.Ob's excellent answer that handles relative files.

function fuser($relativeFile){
  $file = Resolve-Path $relativeFile
  foreach ( $Process in (Get-Process)) {
    foreach ( $Module in $Process.Modules) {
      if ( $Module.FileName -like "$file*" ) {
        $Process | select id, path
      }
    }
  }
}

In use:

> fuser .\node_modules\

  Id Path
  -- ----
2660 C:\Program Files\nodejs\node.exe