- How to View and Close Open Files in Windows Server SMB Share?
- View Open Files on a Shared Network Folder on Windows Server
- How to Find Out Who is Locking a File in a Shared Folder?
- How to Forcibly Close an Open File on a SMB Share?
- Get-SMBOpenFile: Find and Close Open File Handlers Using PowerShell
- How to Close Open Files on Remote Computer Using PowerShell?
- Close-Smb Open File
- Syntax
- Description
- Examples
- Example 1: Close an open file
- Example 2: Close open files for a session
- Example 3: Close open files that match a file name extension
- Parameters
- Windows Server – How To Close Open Files
- View open files on a shared folder
- Usage of Windows Task Manager
- Usage of Resource Monitor
- Powershell cmdlet approach
- Powershell Script approach
- Close A File On Remote Computer Using Command Line
- PsFile usage
- Release a File Lock
- Usage of Third-party apps
How to View and Close Open Files in Windows Server SMB Share?
Windows file server administrators often have to force close the shared files that are open simultaneously by multiple users. This usually happens if the desktop software doesn’t work as expected, the user logs off incorrectly, or when the user opened a file and forgot to close it (went home, on vacation, etc.). In all these cases, the file on the shared network folder is still open (and locked) and cannot be modified by other users. Other users can see such a message when trying to open a locked file (depending on the application used): The document filename is locked for editing by another user. To open a read-only copy of his document, click…
In this article we’ll show you how to get the list of open files on a Windows file server, find out which user locked a file on a shared folder, and how to close (reset) file sessions to unlock open files.
View Open Files on a Shared Network Folder on Windows Server
You can get the list of files opened by users on Windows file server using the built-in Computer Management ( compmgmt.msc ) graphic snap-in.
Open the Computer Management console on your file server (or connect to the server remotely from the management console running on your computer) and go to System Tools -> Shared Folders -> Open files. A list of open files on current SMB server is displayed 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).
You can get the same list of open files using the built-in openfiles.exe console tool. For example, using the following command you can get the Session ID, username and full local path to the open file:
openfiles /Query /fo csv |more
You can display a list of open files on a remote server. For example, you need to list all open files in shared folders on the lon-fs01 host:
openfiles /Query /s lon-fs01 /fo csv
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, run this command:
openfiles /Query /s lon-fs01 /fo csv | find /i «filename.docx»
You can specify only a part of the file name. For example, you need to find out who opened an XLSX file containing “sale_report” in its name. Use the following pipe:
openfiles /Query /s lon-fs01 /fo csv | find /i «sale_report»| find /i «xlsx»
Of course you can find this file in the Computer Management GUI, but it’s less convenient (this console doesn’t provide search feature).
How to Forcibly Close an Open File on a SMB Share?
To close an open file, find it in the list of files in Open File section and select Close Open File in the context menu.
If there are hundreds of open files on your file server, it won’t 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 told, it returns the session ID of the open file. Using this session ID you can force close the file by resetting the SMB connection.
First, you 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
You can forcefully reset all sessions and unlock all files opened by a specific user:
openfiles /disconnect /s lon-fs01/u corp\mjenny /id *
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 can be used to remotely close network connections to an open file.
You can get a list of open files using the Get-SMBOpenFile cmdlet. Close-SmbOpenFile is used to close/reset the connection to a remote file.
To display a list of open files on the Windows SMB server, run the command:
The command returns the file ID, session ID and full file name(path).
You can display a list of open files with user and computer names (IP addresses):
You can list all files opened by a specific user:
Get-SMBOpenFile –ClientUserName «corp\mjenny»|select ClientComputerName,Path
or from a specific computer/server:
Get-SMBOpenFile –ClientComputerName 192.168.1.190| select ClientUserName,Path
You can display a list of open files by pattern. For example, to list all exe files opened from the shared folder:
or open files with a specific name:
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, you can make a simple GUI form for finding and closing open files. The following script will list open files. You should use the built-in filters in the Out-GridView table to find open files for which you want to reset the SMB sessions. Then you 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?
The Get-SMBOpenFile and Close-SmbOpenFile cmdlets can be used to remotely find and close open (locked) files. First, you need to connect to a remote Windows SMB server via a CIM session:
$sessn = New-CIMSession –Computername lon-fs01
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
Confirm closing of the file by pressing Y . As a result, you have unlocked the file. Now other users can open it.
With PowerShell, you can close SMB sessions and unlock all files that a specific user has opened (a user went home and didn’t release the open files). For example, to reset all file sessions of the user mjenny, run this command:
Get-SMBOpenFile -CIMSession $sessn | where <$_.ClientUserName –like "*mjenny*">|Close-SMBOpenFile -CIMSession $sessn
Close-Smb Open File
Closes a file that is open by one of the clients of the SMB server.
Syntax
Description
The Close-SmbOpenFile cmdlet forcibly closes a file that is open by one of the clients of the Server Message Block (SMB) server. This cmdlet can cause data loss to the client for which the file is being closed if the client has not flushed all of the file modifications back to the server before the file is closed.
Examples
Example 1: Close an open file
This command closes a file identified as 4415226383589 that is open by one of the clients of the SMB server.
Example 2: Close open files for a session
This command closes one or more files that are open by one of the clients identified with the session ID 4415226380393 of the SMB server.
Example 3: Close open files that match a file name extension
This command closes, without user confirmation, one or more files that are open by one of the clients of the SMB server and that match the file name extension .DOCX.
Parameters
Runs the cmdlet as a background job. Use this parameter to run commands that take a long time to complete.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Runs the cmdlet in a remote session or on a remote computer. Enter a computer name or a session object, such as the output of a New-CimSession or Get-CimSession cmdlet. The default is the current session on the local computer.
Type: | CimSession [ ] |
Aliases: | Session |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the client computer name to filter the returned open files so that only the matching files are opened.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the name of the user for which the open files are retrieved.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies, if a share is hosted by a windows cluster, the name of the server which is hosting the open files to be retrieved.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Prompts you for confirmation before running the cmdlet.
Type: | SwitchParameter |
Aliases: | cf |
Position: | Named |
Default value: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the file identifier (ID) of the file to forcibly close.
Type: | UInt64 [ ] |
Position: | 1 |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Forces the command to run without asking for user confirmation.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the input object that is used in a pipeline command.
Type: | CimInstance [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.
Type: | SwitchParameter |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Specifies the scope of the open files to be retrieved.
Type: | String [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies a session ID. Only files that have been opened in the specified session will be returned.
Type: | UInt64 [ ] |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the input to this cmdlet. You can use this parameter, or you can pipe the input to this cmdlet.
Type: | SmbInstance |
Accepted values: | Default, CSV, SBL, SR |
Position: | Named |
Default value: | None |
Accept pipeline input: | True |
Accept wildcard characters: | False |
Specifies the maximum number of concurrent operations that can be established to run the cmdlet. If this parameter is omitted or a value of 0 is entered, then Windows PowerShellВ® calculates an optimum throttle limit for the cmdlet based on the number of CIM cmdlets that are running on the computer. The throttle limit applies only to the current cmdlet, not to the session or to the computer.
Type: | Int32 |
Position: | Named |
Default value: | None |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Shows what would happen if the cmdlet runs. The cmdlet is not run.
Windows Server – How To Close Open Files
Here I will describe how to close open server files and processes.
Every system admin on Microsoft Windows Server systems, at least once, will come in a situation that some file is open on a server, and it is needed to check what kind of process or user opened it.
This open files can cause some troubles, like upgrade errors, or reboot hold up etc.
It could be a huge problem, which, if not thought through, can cause the delay of updates, or errors in server maintenance.
More common, but less extreme issues regarding this could come from users. Sometimes, in situations when users leave shared files open on their accounts, some other users, when opening the same file can experience error messages, and cannot access the same file.
This article will show you the way how to deal with that kind of issues, how to find and close open files/process. The operations can be applied to Microsoft Windows Server systems 2008, 2012, 2016 and Windows 10 for workstations.
There are a lot of working methods to deal with that kind of problems, first, one that we will describe is a usage of computer management:
View open files on a shared folder
In a situation of locked files on the server, made by users, this method could come in handy to troubleshoot it.
Use right click on start menu and select Computer Management ( or in start menu search type compmgmt.msc)
The procedure is very simple, and in most cases, it works with no problems.
Click on Shared Folders”, and after that, on Open Files.
That should open the screen with a list of files that are detected as open, the user that opened it, possible locks, and mode that is opened in.
By right click on the wanted file, choose an option, “Close open file”, and that will close it.
With processes and file details, the process is bit different.
Usage of Windows Task Manager
Task Manager will not close opened shared files, but it can close processes on the system.
It can be opened with a combination of keys ctrl, alt, del ( and choose Task Manager), or right-clicking on the taskbar then choose open task manager option.
Under tab processes, you can see all active processes and line it by parameters CPU, Memory etc…
If there is a process that you want to terminate, it can be done by simply right click on the process, and then choose End Process option.
Usage of Resource Monitor
For every system administrator, Resource Monitor is “the tool” that allows control and overview overall system processes and a lot more.
Resource Monitor can be opened by typing “resource monitor” in a start menu search box.
Another option is to open up the task manager, click the performance tab and then click Open Resource Monitor.
When Resource Monitor opens, it will show tabs, and one, needed for this operation is Disk.
It shows disk activity, and processes, files open, PID, read and write bytes per second etc.
If the system is running a lot of “live” processes, it can be confusing, so Resource Monitor offers “stop live monitoring” option, which will stop processes on screen running up and down, and will give you an overview of all processes up to “stop moment”.
Resource monitor offers an overview of opened files paths and processes on the system, and with that pieces of information, it is not a problem to identify and close files or processes.
Powershell cmdlet approach
Of course, PowerShell can do everything, GUI apps can, maybe even better, and in this case, there are several commands, that can and will close all your system’s opened files and processes.
There are more than one solutions with PowerShell scripts, and it is not recommended for administrators without experience in scripting.
For this example, we will show some of the possible solutions with PowerShell usage.
The following examples are applied to Server Message Block (SMB) supported systems, and for systems that do not support SMB, the following examples will show how to close the file with NET file command approach.
In situations where one, or small numbers of exact known open files should be closed, this cmdlet can be used. It is, as usual, used from elevated PowerShell, and applies to a single file ( unsaved data on open files, in all examples, won’t be saved).
There is a variation of cmdlet which allows closing open files for a specific session.
This command does not close a single file, it applies to all opened files under the id of the specific session.
The other variation of the same cmdlet is applying to a file name extension ( in this example DOCX).
The command will check for all opened files with DOCX extension on all system clients and it will force close it. As mentioned before, any unsaved data on open files, will not be saved.
There are a lot more this cmdlet flags, and variations which allow applying a lot of different filters and different approaches to closing open files.
Powershell Script approach
With PowerShell scripts, the process of closing open files and processes can be automated.
Our example script enables closing a file specified by path, that should be inserted In the script.
This way of closing open files is not recommended for administrators without PowerShell scripting experience, and if you are not 100% sure, that you are up to the task, do not use this way.
Close A File On Remote Computer Using Command Line
There are two other ways to close the open files. Either Net File or PSFile (Microsoft utility) could be used to close them. The first command can be ruined by usage of NET File command using the Psexec.exe remotely. The NET command does not support any Remote APIs.
Net file command can list all open shared files and the number of file lock per file. The command can be used to close files and remove locks ( similar to SMB example before) and it is used, similar to example before, when user leave a file open or locked.
It can be done with the following syntax
In this syntax, ID parameter is the identification number of file ( we want to close), and of course, parameter close, represents action we want to apply to ID ( file).
Best practice of NET file command usage is to list open files by running Net File command, which lists all open files and sign it with numbers 0, 1, etc
So when files are listed, the command which will close open files is ( for example),
So command will apply in a way that will close a file signed with number 1.
PsFile usage
PsFile is a third party application, but I will not put it in a list of third parties, as any good system administrator should use it as “normal”.
commands are similar to net file commands, with a difference that it doesn’t truncate long file names, and it can show files opened on remote systems locally.
It uses the NET API, documented in platform tools, and it becomes available by downloading PsTools package.
Usage of PsFile “calls” remote computer with valid username and Password, and with path inserted it will close the open files on the remote system
For Processes opened on the remote system, there is a similar command called PsKill, which on same principle “kills” processes.
Release a File Lock
In some situations, a problem with closing files can be handled by releasing a file lock. There are many examples of users locking their files, and leave it open ( for some reason, the most common type of locked files are excel files).
So all other users get an error message of type: Excel is locked for editing by another user, and get no option to close it or unlock.
As an administrator, you should have elevated rights and with right procedure, that can be fixed easily.
With pressing windows key and R, you will get windows run dialog.
In run dialog type mmc ( Microsoft Management Console).
By going to option File > Add/Remove Snap-in, add a “Shared Folders” snap-in.
If you are already an operating system that has the issue, choose Local Computer option, if not, choose Another computer option and find a wanted computer name.
Expand the Shared Folders, then select open files option.
Choose locked/open file, and close it by right click and selection of Close open file.
The described procedure will unlock and close an open file ( similar as in the first example of an article), and users will be able to access it.
Usage of Third-party apps
There is a lot of third-party apps with the purpose of handling open server files on the market.
We will describe a few of most used ones in this purpose.
Process Explorer – a freeware utility solution from Windows Sysinternals, initially created by Winternals, but acquired by Microsoft. It can be seen as Windows Task Manager with advanced features. One of many features is the close open files feature, and it is highly recommended for Server Administrators and IT professionals.
Sysinternals can be accessed on the following link :
OpenedFilesView – Practically a single executable file application, displays the list of all opened files on your system. For each opened file, additional information is displayed: handle value, read/write/delete access, file position, the process that opened the file, and more.
To close a file or kill a process, right-click any file and select the desired option from the context menu.
It can be downloaded on the following link :
Lockhunter – usually a tool with a purpose of deletion of blocked files ( to recycle bin). It can be a workaround for open files, and it has a feature of listing and unlocking locked files on your system. It is very powerful, and helpful in a situation when system tools fail.
It could be downloaded on following the link: http://lockhunter.com/
Long Path Tool – Long Path Tool is a shareware program provided by KrojamSoftthat, as its name suggests, helps you fix a dozen issues you’ll face when a file’s path is too long. Those issues include not being able to copy, cut, or delete the files in question because its path is too long. With a bunch of features, this could maybe be an “overkill” for this purpose, but it is definitely a quality app for all sysadmins.