Command to know if a file is open on the network

This topic contains 4 replies, has 3 voices, and was last updated by  Vandrey Trindade 3 years, 11 months ago.

  • Author
  • #17200

    Vandrey Trindade


    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?

  • #17202

    Sam Boutros

    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]

  • #17203

    Rob Simmers

    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 )
  • #17207

    Sam Boutros

    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"}
  • #17221

    Vandrey Trindade

    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!

You must be logged in to reply to this topic.