views:

1540

answers:

3

How will I do this in powershell. I like to check which process/application is using the file, so that I can close it.

Thanks.

+2  A: 

You should be able to use the openfiles command from either the regular command line or from powershell.

EDIT: The openfiles built-in tool can be used for file shares or for local files. For local files, you must turn on the tool and restart the machine (again, just for first time use). I believe the command to turn this feature on is: openfiles /local on

Eg (works on Vista x64):

openfiles /query | find "chrome.exe"

That successfully returns file handles associated with Chrome. You can also pass in a file name to see the process currently accessing that file.

Garrett
From what I see that command simply enumerates files that are opened by a user from remote via SMB shares. It won't tell you anything about the process using it.
Joey
You can't tell it from the link, but it looks like Johannes is right. It doesn't work on Vista x64 for me -- says "INFO: No shared open files found."
Joe White
Joe/Johannes: First, do you have the global "maintain objects list" turned on (I think the syntax is "openfiles /local on" IIRC)? Next, are you passing in the "/query" argument, as in the example above (req'd for Vista, it seems)?
Garrett
+3  A: 

You can do this with the SysInternals tool handle.exe. Try something like this:

PS> $handle = handle
PS> foreach ($line in $handle) { 
        if ($line -match '\S+\spid:') {
            $exe = $line
        } 
        elseif ($line -match 'C:\\Windows\\Fonts\\segoeui\.ttf')  { 
            "$exe - $line"
        }
     }
MSASCui.exe pid: 5608 ACME\hillr -   568: File  (---)   C:\Windows\Fonts\segoeui.ttf
...
Keith Hill
Thanks, I can just use handle [filename], to make it simpler.
Marc Vitalis
Where's the fun in that? :-) But yeah, that would be much simpler.
Keith Hill
:( still having problems though. . . it's not that powerful to show if there files (i.e. text files) opened by a certain process.
Marc Vitalis
handle is an external tool though unfortunately
George Mauer
A: 

I try the openfiles command that Garret say. It works flawsless almost for every file that y try... the only files where i can't find a program related are disc images mounted...i have a file mounted with virtual clone drive but doesn't appear in "openfiles /query"...the only way to findit is use the sysinternals software aformention by Keith Hill. It will be nice have a native way to findit.

voodoomsr