Author Posts

July 16, 2014 at 9:33 am


I've created a script that loads three Excel files to three variables into PowerShell to make some queries (the three files are on a network path).
Is there a way to see if those files are open by another user on the network (like we do on Windows Server) using PowerShell?

July 16, 2014 at 10:10 am

You can use openfiles tool to check on open files from the CMD or POWERSHELL

try this:

openfiles /query

if you get a message like

The system global flag 'maintain objects list' needs to be enabled to see local opened files.

try this:

openfiles /local on

you will need to reboot.
After that run something like

$openfiles = openfiles /query

and maniuplate $openfiles to find what you're looking for..

For more information see [url][/url]

July 16, 2014 at 10:32 am

If you are only doing queries, you can force Word to open in read-only mode and you don't need to validate if the documents are open. The MSDN documentation is here:

$Word = New-Object -Com Word.Application
$Word.Visible = $true
$doc=$word.Documents.Open( $document,,$true )

July 16, 2014 at 12:21 pm

This may be more of what you're looking for:

function TestFileLock ($FilePath ){
    $FileLocked = $false
    $FileInfo = New-Object System.IO.FileInfo $FilePath
    trap {Set-Variable -name Filelocked -value $true -scope 1; continue}
    $FileStream = $FileInfo.Open( [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None )
    if ($FileStream) {$FileStream.Close()}
$File = "c:\pagefile.sys"
if (TestFileLock $File) {Write-Output "File '$File' is locked"} else {Write-Output "File '$File' is not locked"}

July 17, 2014 at 4:26 am

Sam Boutros,

Thanks a lot! The second reply worked perfeclty =]

Rob Simmers,

Thanks for your reply.
But I needed to know if the file was open to alert the user that the file must be changed on that moment.
So the info can be outdated during the query.

This is the best forum ever. Thank you very much!