Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / Windows Server 2012 R2 / How to View and Close Open Files in Windows Server SMB Share?

October 20, 2022 PowerShellWindows 10Windows Server 2012 R2Windows Server 2016

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.

Contents:
  • 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?

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).

List of Open Files on Windows Server 2012 R2 shared folders

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

Openfiles cli tool to manage open files

When a user remotely accesses a folder or file in a shared network folder on the server, a new SMB session is created. You can manage open files using these session IDs.

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

The openfiles command also allows you to view the list of locally opened files. To use it, enable the “Maintain Objects List” option using the command: openfiles /local on, and reboot your server. After that, the openfiles command will display files opened by local processes (it is recommended to use this mode only for debugging purposes, since it can negatively affect server performance).

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"

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

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.

close open file using computer managment console GUI

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

Openfiles - Disconnect user by session id

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

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

Note. Forcibly closing a file opened by a client on an SMB server may result in the loss of unsaved data. Therefore, use the openfiles /disconnect command or the Close-SMBOpenFile cmdlet (discussed below) very carefully.

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

New cmdlets to manage shares and files on an SMB server appeared in the 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:

Get-SMBOpenFile

get-smbopenfile powershell cmdlet

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):

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

powershell: list smb open files with usernames

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:

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, 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

powershell gui script to close open files using out-gridview

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

You can also connect to a remote server to run PowerShell 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

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

PowerShell Get-SMBOpenFile - Close-SMBOpenFile

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

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

7 comments
4
Facebook Twitter Google + Pinterest
previous post
Configuring USB Devices Passthrough from VMWare ESXi to a Virtual Machine
next post
How to Mount Google Drive or OneDrive in Linux?

Related Reading

How to Connect VPN Before Windows Logon

November 14, 2023

Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

November 7, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

7 comments

Aleksandr April 22, 2019 - 9:16 am

How can an ordinary user be allowed to use these cmdlets?

Reply
admin May 14, 2019 - 1:53 pm

No, you need local admin permissions to run these cmdlets.

Reply
Scott Davis January 7, 2020 - 1:56 pm

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???

Reply
admin January 15, 2020 - 8:14 am

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.

Reply
Piotr September 17, 2020 - 1:47 pm

Hi is there a way to view computer name instead IP while using “ClientComputerName”

Reply
admin September 17, 2020 - 5:03 pm

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
}

Reply
Jon A June 23, 2021 - 5:22 am

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.

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • Managing Saved Passwords Using Windows Credential Manager
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top