A Windows file server admin can list the files that are open in a shared folder and force locked files to close. If a user opens a file in a shared SMB folder on the file server for writing and forgets to close it, this means that other users won’t be able to modify the file.
In this article, we’ll look at how to get a list of the files that are open on a Windows file server, the users that are using them, and how to reset file sessions to unlock open files.
View Open Files on Windows Server
You can use the graphical Computer Management snap-in to get a list of files open over the network in Windows
- Run the
compmgmt.msc
console and go to System Tools -> Shared Folders -> Open files; - A list of open files is displayed on the right side of the window. Each entry shows the local path to the file, the user account name, the number of locks, and the mode in which the file is opened (Read or Write+Read).
You can also list the files open on the server from the command line:
openfiles /Query /fo csv
The command returns the session number, user name, number of locks, and the full local path to the file.
A more convenient way to work with the list of files that are open on the server is to use the Get-SmbOpenFile PowerShell cmdlet:
Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessionID
The command output includes the user name, the name (IP address) of the remote computer from which the file was opened, the file name, and the SMB session ID.
How to See Who Has Locked an Open File on Windows
Users will see a warning if they try to open a locked file.
The document filename is locked for editing by another user. To open a read-only copy of his document, click…
To remotely identify the user who has locked the name.docx file in a shared folder on the lon-fs01 server, run the command
openfiles /query /s lon-fs01 /fo csv | find /i "name.docx"
/i
switch is used to make the filename case insensitive when searching.You can only specify part of the filename. For example, to find out who has opened a .XLSX file that contains the string sale_report in its name, use the following pipe:
openfiles /Query /s lon-fs01 /fo csv | find /i "sale_report"| find /i "xlsx"
To get information about the user who opened the file, you can also use PowerShell.
List all EXE files opened from the shared folder:
Get-SmbOpenFile | Where-Object {$_.Path -Like "*.exe*"}
Search for open files by part of their name:
Get-SmbOpenFile | Where-Object {$_.Path -Like "*reports*"}
List all files opened by a specific user:
Get-SMBOpenFile –ClientUserName "corp\mjenny"|select ClientComputerName,Path
Find files opened from a specified remote computer:
Get-SMBOpenFile –ClientComputerName 192.168.1.190| select ClientUserName,Path
Close Open Files on Windows Server
You can use the Computer Management console to close an open file. Locate the file in the list in the Open Files section and select ‘Close Open File’ from the menu.
Finding the file you want in the graphical console can be difficult when there are hundreds of files open on the server. It’s better to use command line tools in this case.
You can close a file by specifying its SMB session ID. Get file session ID:
Openfiles /Query /fo csv | find /i "report2023.xlsx"
The received SMB session ID can now be used to force a user to disconnect:
openfiles /disconnect /ID 3489847304
SUCCESS: The connection to the open file "D:\docs\public\REPORT2023.XLSX" has been terminated.
If you want to forcibly reset all sessions and release all files opened by a specific user:
openfiles /disconnect /s lon-fs01/u corp\mjenny /id *
You can use the PowerShell cmdlet Close-SmbOpenFile to close an open file handler by session ID.
Close-SmbOpenFile -FileId 4123426323239
Find and close an open file with a one-line command:
Get-SmbOpenFile | where {$_.Path –like "*annual2020.xlsx"} | Close-SmbOpenFile -Force
To confirm the reset of the session and release the open file, press Y
-> Enter
.
Add the -Force
parameter to the last command to close the file without warning.
The Out-GridView cmdlet allows you to create a simple graphical form for finding and closing open files. The following script will list open files. The administrator must use the filters in the Out-GridView table to find and select the required files, and then click the OK button to forcibly close them.
Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessionID| Out-GridView -PassThru –title "Select Open Files"|Close-SmbOpenFile -Confirm:$false -Verbose
openfiles /disconnect
and Close-SMBOpenFile
commands should be used with care.How to Remotely Close Open Files with PowerShell
The Get-SMBOpenFile
and Close-SmbOpenFile
cmdlets can be used to remotely find and close open (locked) files. The first step is to establish a CIM session with a remote Windows file server:
$sessn = New-CIMSession –Computername lon-fs01
The following command finds and closes the session for the open file pubs.docx.
Get-SMBOpenFile -CIMSession $sessn | where {$_.Path –like "*pubs.docx"} | Close-SMBOpenFile -CIMSession $sessn
Confirm closing of the file by pressing Y
. This unlocks the open file, and other users can open it.
You can use PowerShell to close SMB sessions and unlock all files that a specific user has opened (for example, if a user goes home and doesn’t release the open files.). The following command resets all file sessions of the user mjenny:
Get-SMBOpenFile -CIMSession $sessn | where {$_.ClientUserName –like "*mjenny*"}|Close-SMBOpenFile -CIMSession $sessn
7 comments
How can an ordinary user be allowed to use these cmdlets?
No, you need local admin permissions to run these cmdlets.
Any idea why the list of opened files would report invalidly? In our domain environment I am seeing in the GUI that I myself have opened files that I have never opened???
You can see from which computers the files in the shared network folder are open.
Explore processes on these computers using the FileMon or Resource Monitor. Perhaps these are antivirus processes or something like that.
Hi is there a way to view computer name instead IP while using “ClientComputerName”
You can resolve IP address to hostname with PowerShell:
$smbfiles=Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessionID
ForEach ($file in $smbfiles)
{
[System.Net.Dns]::GetHostEntry($file.ClientComputerName).HostName
}
Interesting piece of code using the Out-Gridview method. Problem with it though I discovered. I ran it on our server, and from a remote session (after everyone had gone home). I selected a half dozen open files that were left open and that was out of about 20 or so. After selecting the OK button, it generated many lines of error messages between lines of performing the removal action. Unfortunately, when it stopped, it closed everything except for two files.