1

Problem:

Hi, I am trying to find all the processes which are using(reading or writing) a particular file using Powershell only.

I have tried using Get-Process with where-Object filter but it is not working. I have very less experience in Windows hence finding it difficult to understand where to look and how to achieve.

Any help would be appreciated.

Thanks

Alex
  • 25
  • 1
  • 5
  • What are you [trying to achieve](https://meta.stackexchange.com/q/66377)? Finding out process that holds a file lock is easily done with, say, Sysinternals' Handle.exe. – vonPryz May 18 '18 at 09:23
  • I want to write my own function to check if the file is read or write by the process (not just the lock). And I want to do it in Powershell only. @vonPryz – Alex May 18 '18 at 09:33
  • 2
    If you cannot use 3rd party tools, then P/Invoke is needed to call Win32 API. Pick a [C# example](https://stackoverflow.com/q/3899864) and work from that. – vonPryz May 18 '18 at 09:54
  • @vonPryz Oh! does that mean that it is not possible to create a simple function in PowerShell to check the processes which have the file open with them? – Alex May 18 '18 at 14:29
  • As far as I know, handles are not exposed to managed code. Thus one needs to either call Win32 API via P/Invoke or use 3rd party tools and parse the output. – vonPryz May 21 '18 at 06:03

1 Answers1

0

Information in this blog post should be able to help out - http://jdhitsolutions.com/blog/powershell/3744/friday-fun-find-file-locking-process-with-powershell/

The Sysinternals suite has a tool called 'Handle.exe' which if you pass it a file, will let you know what process has a handle to that file (if one exists).

Adam Parsons
  • 506
  • 2
  • 9