How to close open files on Windows Server 2022

Every Microsoft Windows Server system administrator will, at least once, encounter a situation where a file is open on a server and need to check which process or user opened it.

These open files can cause problems such as upgrade errors, maintenance errors, reboot hold up, etc.

A typical example is an end-user opening a shared file, and no other users can access it.

Below we will discuss different options to close open files and processes.

These steps work with Microsoft Windows Server 2008, 2012, 2016, 2019, 2022, Windows 10, and Windows 11.

VIEW OPEN FILES ON A SHARED FOLDER

Right-click on the start menu and select Computer Management.

Alternatively, search for:

 compmgmt.msc

Click on Shared Folders, and then Open Files.

This menu displays open shared files, the user who opened it, possible locks, and the mode opened in.

Right-click on a file and select Close open File.

USE WINDOWS TASK MANAGER

Task Manager cannot close opened shared files, but it can end running processes on the system.

Task Manager can be accessed via Control + Alt + Delete and select Task Manager, or right-click the taskbar and select Task Manager.

Under the Processes tab, you will see all running processes. To terminate a running process, right-click it and select End Process.

RESOURCE MONITOR

Resource Monitor is accessed by typing “resource monitor” in a start menu search box or opening the task manager, clicking the performance tab, and clicking Open Resource Monitor.

When Resource Monitor opens, it will show tabs, and one, needed for this operation is Disk.

The Resource Monitor shows disk activity and processes, files that are open, process ID number, read and write bytes per second, etc. This information is helpful to identify open files and running processes.

POWERSHELL CMDLET

In most cases, PowerShell is better than GUI-based applications. Multiple commands can be used to close open files and processes.

There is more than one solution with PowerShell scripts, and administrators without experience in scripting are recommended to use GUI options instead.

Below are some possible solutions with PowerShell.

The following examples are for Server Message Block (SMB) supported systems.

This cmdlet can be used when a small number of known open files should be closed. It is, as usual, used from elevated PowerShell and applies to a single file ( note that all unsaved data on open files will not be saved).

 Close-SmbOpenFile -FileId ( id of file )

Confirm

Are you sure you want to perform this action?

Performing operation ‘Close-File’ on Target ‘( id of file)’.

[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “Y”): N

Closing files for a specific session can be achieved with the below-edited script.

 Close-SmbOpenFile -SessionId ( session id )

This command closes all open files under the ID of the specific session.

The other variation of the same cmdlet applies to a file name extension ( for example, DOCX).

The command will check all opened files with DOCX extension on all system clients and force close it. Unsaved changes on open files will not be saved.

 Get-SmbOpenFile | Where-Object -Property ShareRelativePath -Match ".DOCX" | Close-SmbOpenFile -Force

POWERSHELL SCRIPT

PowerShell scripts can automate closing open files and stopping running processes.

The below example script enables closing a file specified by path. This path needs to be provided in the script.

There is more than one solution with PowerShell scripts, and administrators without experience in scripting are recommended to use GUI options instead.

$blok = {$adsi = [adsi]"WinNT://./LanmanServer"
$resources = $adsi.psbase.Invoke("resources") | Foreach-Object {
  New-Object PSObject -Property @{
  ID = $_.gettype().invokeMember("Name","GetProperty",$null,$_,$null)

Path = $_.gettype().invokeMember("Path","GetProperty",$null,$_,$null)

OpenedBy = $_.gettype().invokeMember("User","GetProperty",$null,$_,$null)

LockCount = $_.gettype().invokeMember("LockCount","GetProperty",$null,$_,$null)

}

}

$resources | Where-Object { $_.Path -like '*smbfile*'} |ft -AutoSize

$resources | Where-Object { $_.Path -like '*smbfile*'} | Foreach-Object { net files $_.ID /close }

}

Invoke-Command -ComputerName pc1 -ScriptBlock $blok 

COMMAND LINE

From a standard Command Prompt, the Net File command can be used to close open files. To run this remotely, Psexec.exe is required

Net File command can list all open shared files and the number of file locks per file. This command can be used to close files and remove file locks ( similar to the previous SMB example).

 C:>net file [id [/close]] 

RELEASE A LOCKED FILE

Should users encounter an error that a file is locked, an administrator will be able to resolve this by opening the Microsoft Management Console

Search for:

 mmc

Select File, then Add/Remove Snap-in and add the Shared Folders snap-in.

This snap-in can be run on a local computer or a remote computer.

Select locked/open file, right-click and select Close open file.

THIRD-PARTY TOOLS

Below is a list of some of the most commonly used third-party applications for managing open files.

PSFILE

PsFile is part of the PSTools package from Microsoft Sysinternals. This tool is similar to the Net File mentioned earlier.
This tool gives the ability to connect to a remote server and see open files.
Note: Unlike Net File, this tool cannot truncate file names.

 psfile [\\RemoteComputer [-u Username [-p Password]]] [[Id | path] [-c]]

Sysinternals website: https://docs.microsoft.com/en-us/sysinternals/

Process Explorer

Another featured application from Microsoft Sysinternals. This advanced Task Manager can close open files, amongst many other features.
Sysinternals website: https://docs.microsoft.com/en-us/sysinternals/

OpenedFilesView

This single-executable application shows all open files and gives the ability to close open files and end processes. Though not explicitly listed on the website, this has been successfully tested on Windows Server 2022.
OpenedFilesView website: https://www.nirsoft.net/utils/opened_files_view.html

Lockhunter

Primarily used for deleting blocked files, it is also possible to use Lockhunter as a workaround to unlock files. Though not explicitly listed on the website, this has been successfully tested on Windows Server 2022.
Lockhunter website: https://lockhunter.com/

UnLock IT

Another popular tool designed to close open and locked files is UnLockIT. Though not explicitly listed on the website, this has been successfully tested on Windows Server 2022.

UnLock IT website: https://emcosoftware.com/unlock-it

Long Path Tool

Unlike the other utilities listed here, Long Path Tool is a shareware program. As the name suggests, it helps fix issues when a file path is too long. Those issues include not being able to copy, cut, or delete the files in question because their path is too long. This application does require the Dot Net 3.5 feature to be installed in order to run on Microsoft Windows Server 2022.
With so many features, the functionality in this tool could be overkill for this specific purpose, but worth mentioning as it is a quality tool for all system administrators.

Long Path Tool website: https://longpathtool.com/

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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