5

I am trying to close all of the open files in the shared folders of a server via PowerShell script. I found the following script which only close files on one drive (F:), however, this server has 3 drives (F:\, G:\, H:) which I want to close all.

net files | 
    where   { $_.Contains( "F:\" ) } |
    foreach { $_.Split( ' ' )[0] }   |
    foreach { net file $_ /close }

Is there a way to add missing drives into this script or I have to use the same script separately for each drive?

I tried { $_.Contains( "F:\", "G:\", "H:\" ) } but didn't work

Thank you for your help!

eccoripo
  • 53
  • 1
  • 3

1 Answers1

8

The command you are using is not a native PowerShell command. However, PowerShell does come with a lot of functions to control SMB connections and shares.

What you are looking for could be this:

Get-SMBOpenFile | Close-SMBOpenFile -Force

This will close all files opened via file shares. You can also manage SMB Connections (Get-SMBConnection) and other things. With Get-Command *smb* you will get a list of all SMB related commands.

Tobias
  • 1,236
  • 1
  • 13
  • 25
  • Yep, it works. However, I get a confirmation window (Are you sure you want to perform this action?). Is there a way to override it? – eccoripo Oct 29 '18 at 17:33
  • Add the -Force parameter to the close command. I added it into my answer. – Tobias Oct 29 '18 at 17:36