Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Windows Server 2019 / How to View and Close Open Files on Windows Server

December 8, 2023

How to View and Close Open Files on Windows Server

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.

Contents:
  • View Open Files on Windows Server
  • How to See Who Has Locked an Open File on Windows
  • Close Open Files on Windows Server
  • How to Remotely Close Open Files with PowerShell

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

  1. Run the compmgmt.msc console and go to System Tools -> Shared Folders -> Open files;
  2. 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).List of Open Files on Windows Server 2012 R2 shared folders

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.

Openfiles cli tool to manage open files

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.

powershell: list smb open files with usernames

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"

The /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.

close open file using computer managment console GUI

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

powershell gui script to close open files using out-gridview

Note. Forcibly closing a file that is open on a file server will cause the user to lose any unsaved data. So, the openfiles /disconnect and Close-SMBOpenFile commands should be used with care.

How to Remotely Close Open Files with PowerShell

The Get-SMBOpenFile and Close-SmbOpenFilecmdlets 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

You can also use the PSRemoting cmdlets (Enter-PSSession or Invoke-Command) to connect to a remote server and run commands.

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.

PowerShell Get-SMBOpenFile - Close-SMBOpenFile

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

The Process Explorer tool can be used to close local open file handlers in Windows.
7 comments
5
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows Server 2019
previous post
Configuring USB Devices Passthrough from VMWare ESXi to a Virtual Machine
next post
Selecting the Number of vCPUs and Cores for a Virtual Machine

Related Reading

How to Convert (Upgrade) Windows Server Evaluation to...

March 15, 2024

How to Clean Up System Volume Information Folder...

March 17, 2024

Protecting Remote Desktop (RDP) Host from Brute Force...

February 5, 2024

Managing Administrative Shares (Admin$, IPC$, C$) on Windows

March 15, 2024

How to Install Only Specific Apps in Office...

March 12, 2024

Using Credential Manager on Windows: Ultimate Guide

March 15, 2024

How to Check, Enable or Disable SMB Protocol...

March 11, 2024

How to Connect L2TP/IPSec VPN Server From Windows

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

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • Using Credential Manager on Windows: Ultimate Guide
  • Managing Printers and Drivers on Windows with PowerShell
  • PowerShell: Get Folder Size on Windows
  • Protecting Remote Desktop (RDP) Host from Brute Force Attacks
Footer Logo

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


Back To Top