Skip to main content

How to List Open Files on a Remote Share in Batch Script

A common challenge for system administrators is troubleshooting "file in use" errors or determining who has a file open on a network share before performing maintenance or a server reboot. Windows provides a powerful, built-in command-line utility called OPENFILES that can query a remote server and list all currently open shared files and the users who have them open.

This guide will teach you how to use the OPENFILES command from a batch script to query a remote server. You will learn the critical prerequisites that must be met for the command to work, how to format the output for scripting, and how to create a practical script to find out exactly who has a specific file locked.

CRITICAL: Prerequisites for Using OPENFILES

For the OPENFILES command to work remotely, several conditions must be met:

  1. Administrator Privileges: You must run your batch script from a command prompt with local administrator rights on the remote server.
  2. Object Auditing Must Be Enabled (on the remote server): This is the most common point of failure. By default, Windows Server does not track this information for performance reasons. You must enable it on the remote server you want to query.
    • Open a command prompt as an Administrator on the remote server.
    • Run the command: openfiles /local on
    • You must reboot the server for this change to take effect. This is a one-time setup. Once enabled, you can query the server at any time.
  3. Firewall Rules: The remote server's firewall must allow incoming "File and Printer Sharing" traffic, as the command uses Remote Procedure Calls (RPC) to communicate.

The Core Command: OPENFILES

The OPENFILES.exe utility is the standard tool for querying and disconnecting open file handles. We will focus on its querying capabilities.

Key OPENFILES Parameters for Querying

  • /Query: The main switch to list open files.
  • /S <server>: Specifies the remote Server to connect to (e.g., /S MyFileServer).
  • /U <user> and /P <password>: Allows you to specify alternative credentials. (Warning: Not secure in scripts).
  • /FO <FORMAT>: Format Output. This is essential for scripting.
    • TABLE: The default, human-readable table.
    • LIST: A simple list of properties for each file.
    • CSV: Comma-Separated Values. This is the best format for scripting.
  • /V: Verbose. Provides more detailed information.

Basic Example: Listing All Open Files on a Server

This command will connect to FILE-SRV-01 and display a table of all files that are currently open over a network share.

@ECHO OFF
REM Run as an administrator with rights to the remote server.
ECHO --- Listing all open files on FILE-SRV-01 ---
ECHO.
OPENFILES /Query /S FILE-SRV-01

The default table output is useful for a quick glance.

Files opened remotely on FILE-SRV-01:
ID Accessed By Type Open File (Path\executable)
----------- --------------- ---------- ------------------------------------------
67108867 jdoe Windows D:\Shares\Public\Annual Report.docx
67108872 asmith Windows D:\Shares\Finance\Q4_Budget.xlsx

How to Capture and Parse the Results in a Script

To use this information in a script, you must use the /FO CSV format and parse it with a FOR /F loop.

@ECHO OFF
SETLOCAL
SET "SERVER=FILE-SRV-01"

ECHO --- Capturing Open File Information from %SERVER% ---
ECHO.

REM 'skip=1' ignores the header. 'tokens=2,4' grabs the "Accessed By" and "Open File" columns.
FOR /F "skip=1 tokens=2,4 delims=," %%A IN (
'OPENFILES /Query /S %SERVER% /FO CSV'
) DO (
ECHO User "%%~A" has the file "%%~B" open.
)

ENDLOCAL
note

%%~A and %%~B are used to remove the quotes that the CSV format adds.

Common Pitfalls and How to Solve Them

Problem: "The system global flag 'maintain objects list' needs to be enabled."

This is the most common error and it means the prerequisite from Step 1 was not met.

Solution: You must log in to the remote server, open an administrator command prompt, run openfiles /local on, and then reboot the server. There is no remote way to enable this setting.

Problem: "Access is denied." (Permissions)

This error means the account running the script does not have administrator rights on the remote machine.

Solution: Run the script from an elevated command prompt as a user who is a member of the local Administrators group on the target server (e.g., a Domain Admin).

Problem: The RPC Server is Unavailable (Firewall)

This error indicates a network connectivity issue, almost always a firewall.

Solution: Ensure the "File and Printer Sharing" inbound rule is enabled in the Windows Firewall on the remote server.

Practical Example: A "Who Has This File Open?" Script

This is the most practical use case. The script takes a server name and a part of a filename as arguments and reports exactly who has that file open.

@ECHO OFF
SETLOCAL
SET "ServerName=%~1"
SET "FileNamePart=%~2"

IF "%ServerName%"=="" (ECHO [ERROR] No server name specified. & GOTO :Usage)
IF "%FileNamePart%"=="" (ECHO [ERROR] No file name specified. & GOTO :Usage)

ECHO --- Searching for who has '%FileNamePart%' open on %ServerName% ---
ECHO.

REM Run the openfiles command and pipe to findstr to filter the results.
FOR /F "skip=1 tokens=2,4 delims=," %%A IN (
'OPENFILES /Query /S %ServerName% /FO CSV ^| findstr /I /C:"%FileNamePart%"'
) DO (
ECHO User: %%~A
ECHO File: %%~B
ECHO.
)

GOTO :EOF

:Usage
ECHO Usage: %~n0 <ServerName> <PartialFileName>
ECHO Example: %~n0 FILE-SRV-01 "Annual Report"

Conclusion

The OPENFILES command is an indispensable tool for remotely managing file shares. While it requires important one-time setup on the target server, it provides complete visibility into who is accessing files.

Key takeaways for using it in a script:

  • The remote server must have object auditing enabled (openfiles /local on) and be rebooted.
  • You must run the script as an administrator with permissions on the remote server.
  • For scripting, always use the /FO CSV switch to produce clean, parsable output.
  • Combine OPENFILES with findstr to create powerful scripts that can pinpoint exactly who has a specific file locked.