Query or display open files, disconnect files opened by network users.
Syntax Openfiles.exe /query [/s Computer [/u Domain\User [/p Password]]] [/fo {TABLE|LIST|CSV}] [/nh] [/v] Openfiles.exe /disconnect [/s Computer [/u Domain\User [/p Password]]] {[/id OpenFileID]|[/a UserName]|[/o OpenMode]} [/se SessionName] [/op OpenFileName] Key /s The name or IP address of a remote computer. (Do not use backslashes.) default=local computer. /u Run the command with the account permissions of user. Default=current logged on user. /p The password of the user account specified with /u. /fo The format to use for the query output. Valid values are TABLE, LIST, and CSV. Default=TABLE. /nh No column headers in the output. Valid only when /fo = TABLE or CSV. /id Disconnect the file opened with the specified numeric OpenFileID on computer Use openfiles.exe /query to learn the file ID. The wildcard (*) can be used to disconnect all open files on computer. /a Disconnect all open files that were accessed by user on computer. The wildcard (*) can be used to disconnect all open files on computer. /o Disconnect all open files with the specified OpenMode on the computer specified by the /s parameter. The OpenMode parameter includes the Read/Write and Read modes. The wildcard (*) can be used to disconnect all open files on computer. /se Disconnect all open files that were created by the specified session on computer. Wildcards (*) can be used. (the /se option is not available under Windows 7) /op Disconnect the open file that was created with the specified OpenFileName on computer The wildcard (*) can be used to disconnect all open files on computer. /v Display verbose information in the output. /? Help.
Administrator privileges are required to run the OPENFILES command. We can use this fact to detect if the current user is an Admin ( OPENFILES >nul 2>nul will set %ERRORLEVEL% = 1 if the user is not an administrator.) However this will fail when running under WOW64 e.g. running a 32 bit installer on an 64 bit system, so an alternative is to use NET SESSION.
Running openfiles.exe from within powershell allows the output to be easily assigned to a variable.
Examples
From the CMD shell:
C:\> openfiles /query /fo list /v
C:\> openfiles /query /s Server64 /u SS64Dom\FileAdmin /p password1
C:\> openfiles /disconnect /o read/write
C:\> openfiles /disconnect /op "c:\work\finance.xls"
C:\> openfiles /disconnect /s Server64 /u SS64Dom\FileAdmin /id 5
C:\> openfiles /disconnect /s Server64 /u SS64Dom\FileAdmin /p password1 /id *
From PowerShell:
PS C:\> openfiles /query
PS C:\> openfiles /query /fo table /nh
PS C:\> $file_list = openfiles /query /s Server64 /fo CSV /v /nh
PS C:\> openfiles /query /s Server64 /fo CSV /v | ConvertFrom-Csv | Out-GridView
PS C:\> openfiles /disconnect /id 1
PS C:\> openfiles /disconnect /a mike
“Love is a sudden revelation: a kiss is always a discovery” - Anon
Related:
NET FILE - Force an open file to close.
PsFile - Show files opened remotely, or close an open file.
PsList - List detailed information about processes.
TASKLIST - List running applications and services.
Equivalent bash command (Linux): inotify - file-monitoring mechanism.