Bobcares

View and Close Open Files in Windows Server SMB Share

by | Dec 2, 2020

Windows file server administrators often have to force close the shared files that are open simultaneously by multiple users.

Today, let’s see how our Support Engineers view Open Files in Windows server and how we close (reset) file sessions to unlock open files.

 

View Open Files on a Shared Folder on Windows Server

We can get the list of open files on Windows file server using the built-in Computer Management (compmgmt.msc) graphic snap-in.

First, we open the Computer Management console on file server and go to System Tools -> Shared Folders -> Open files. We can see a list of open files on current SMB server on the right side of the window.

The list contains the local path to the file, the name of the user account that opens the file, the number of locks and the mode in which the file is opened (Read or Write+Read).

Open Files in Windows Server

We can get the same list of open files using the built-in openfiles.exe console tool.

For example, using the following command we can get the Session ID, username and full local path to the open file:

openfiles /Query /fo csv |more

When a user remotely access a file in a shared network folder, a new SMB session is created. We can manage open files using these session IDs.

We can display a list of open files on a remote server.

For example, to list all open files in shared folders on the lon-fs01 host we use:

openfiles /Query /s lon-fs01 /fo csv

The openfiles command also allows us to view the list of locally opened files. To view it, we enable the “Maintain Objects List” option using the command: openfiles /local on, and reboot the server.

it is recommended to use this mode only for debugging purposes, since it can negatively affect server performance.

[Need assistance to view Open Files? We’d be happy to help]

 

How to Find Out Who is Locking a File in a Shared Folder?

To identify the user who opened (locked) the filename.docx file on the shared network folder on the remote server lon-fs01, we run the command:

openfiles /Query /s lon-fs01 /fo csv | find /i “filename.docx”

The /i key is to perform case-insensitive file search.

We can specify only a part of the file name.

For example, We need to find out who opened an XLSX file containing “sale_report” in its name. To find, we use the following pipe:

openfiles /Query /s lon-fs01 /fo csv | find /i “sale_report”| find /i “xlsx”

Of course, we can find this file in the Computer Management GUI, but it is less convenient.

 

How to Forcibly Close an Open File on a SMB Share?

To close an open file, we find it in the list of files in ‘Open File’ section and select ‘Close Open File’ in the context menu.

Open Files in Windows Server

If there are hundreds of open files on the file server, it will not be easy to find the specific file in the console. It is more convenient to use the Openfiles command line tool.

As we have already mentioned, it returns the session ID of the open file. By using this session ID we can force close the file by resetting the SMB connection.

First, we need to find the session ID of the open file:

openfiles /Query /s lon-fs01 /fo csv | find /i “farm”| find /i “.xlsx”

Disconnect the user from file using the received SMB session ID:

openfiles /Disconnect /s lon-fs01 /ID 617909089

We can forcefully reset all sessions and unlock all files opened by a specific user:

openfiles /disconnect /s lon-fs01/u corp\mjenny /id *

Usually, force closing a file opened by a client on an SMB server may result in the loss of unsaved data. Hence, we carefully use the openfiles /disconnect command or the Close-SMBOpenFile cmdlet0.

[Need help to Close an Open File on a SMB Share? We are available 24*7]

 

Get-SMBOpenFile: Find and Close Open File Handlers Using PowerShell

New cmdlets to manage shares and files on an SMB server appeared in PowerShell version for Windows Server 2012/Windows 8. These cmdlets are to remotely close network connections to an open file.

We can get a list of open files using the Get-SMBOpenFile cmdlet. Close-SmbOpenFile is to close/reset the connection to a remote file.

To display a list of open files on the Windows SMB server, we run the command:

Get-SmbOpenFile | Format-List

The command returns the file ID, session ID and full file name (path).

We can display a list of open files with user and computer names (IP addresses):

Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessioID

We can list all files opened by a specific user:

Get-SMBOpenFile –ClientUserName “corp\bob”|select ClientComputerName,Path

or from a specific computer/server:

Get-SMBOpenFile –ClientComputerName 192.168.1.190| select ClientUserName,Path,/pre>

We can display a list of open files by pattern. For example, to list all exe files opened from the shared folder:

Get-SmbOpenFile | Where-Object {$_.Path -Like “*.exe*”}

or open files with a specific name:

Get-SmbOpenFile | Where-Object {$_.Path -Like “*reports*”}

The Close-SmbOpenFile cmdlet is used to close the open file handler. You can close the file by ID:

Close-SmbOpenFile -FileId 4123426323239

But it is usually more convenient to close the file by name:

Get-SmbOpenFile | where {$_.Path –like “*annual2020.xlsx”} | Close-SmbOpenFile -Force

With the Out-GridView cmdlet, we can make a simple GUI form for finding and closing open files.

The following script will list open files. We should use the built-in filters in the Out-GridView table to find open files for which we want to reset the SMB sessions.

Then, we need to select the required files and click OK. As a result, the selected files will be forcibly closed.

Get-SmbOpenFile|select ClientUserName,ClientComputerName,Path,SessionID| Out-GridView -PassThru –title “Select Open Files”|Close-SmbOpenFile -Confirm:$false -Verbose

 

How to Close Open Files on Remote Computer Using PowerShell?

Now, let us see how our Support Engineers close Open Files on Remote Computer using PowerShell.

The Get-SMBOpenFile and Close-SmbOpenFile cmdlets can be used to remotely find and close open (locked) files.

First, we need to connect to a remote Windows SMB server via a CIM session:

$sessn = New-CIMSession –Computername lon-fs01

We can also connect to a remote server to run PorwerShell commands using the PSRemoting cmdlets:

Enter-PSSession or Invoke-Command.

The following command will find the SMB session for the open file pubs.docx and close the file session:

Get-SMBOpenFile -CIMSession $sessn | where {$_.Path –like “*pubs.docx”} | Close-SMBOpenFile -CIMSession $sessn

We confirm closing of the file by pressing Y. As a result, we have unlocked the file. Now, other users can open it.

To remove the confirmation of force closing a file on a SMB server, we use the -Force key.

With PowerShell, we can close SMB sessions and unlock all files that a specific user has opened.

For example, to reset all file sessions of the user bob, we run the command:

Get-SMBOpenFile -CIMSession $sessn | where {$_.ClientUserName –like “*bob*”}|Close-SMBOpenFile -CIMSession $sessn

[Get our 24/7 support. Our server specialists will keep your servers fast and secure.]

 

Conclusion

In short, this usually happens if the desktop software does not work as expected, the user logs off incorrectly, or when the user opened a file and forgot to close it. Today, we saw how our Supprt Engineers go about with this issue.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.