1

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.

Vikash Pandey
  • 5,407
  • 6
  • 41
  • 42
mikemaccana
  • 110,530
  • 99
  • 389
  • 494

2 Answers2

1

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?

Community
  • 1
  • 1
Eldo.Ob
  • 774
  • 4
  • 16
  • Thanks @Eldo.Ob! I notice this only seems to work with a full path - any way to make it work with a relative folder name? – mikemaccana Aug 25 '16 at 15:53
  • You're welcome :) No it's not a full path. The script gets all processes, that is running in the given directory or subdirectories. So if you set "D:\" into the var $lockedFolder, you'll get all processes running in Directory D:\ or subdirectories. Just try: `$lockedFolder="C:\Windows\System32" Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id + " FullName: " + $processVar.FileName }}}` – Eldo.Ob Aug 25 '16 at 18:13
  • oh I made a mistake: `$lockedFolder="C:\Windows\System32" Get-Process | %{$processVar = $_;$_.Modules | %{if($_.FileName -like "$lockedFolder*"){$processVar.Name + " PID:" + $processVar.id + " FullName: " + $_.FileName }}}` – Eldo.Ob Aug 25 '16 at 18:37
  • I mean you have to set $lockedFolder to a full path, not a relative one - I'd like to make this into a short function. – mikemaccana Aug 26 '16 at 07:59
0

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
mikemaccana
  • 110,530
  • 99
  • 389
  • 494