Command to know if a file is open on the network

Welcome Forums General PowerShell Q&A Command to know if a file is open on the network

This topic contains 4 replies, has 3 voices, and was last updated by

4 years, 4 months ago.

  • Author
  • #17200

    Points: 42
    Rank: Member


    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

    Points: 76
    Rank: Member

    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

  • #17203

    Points: 305
    Helping Hand
    Rank: Contributor

    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

    Points: 76
    Rank: Member

    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

    Points: 42
    Rank: Member

    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!

The topic ‘Command to know if a file is open on the network’ is closed to new replies.