Power shell script to close shared file. RRS feed

  • Question

  • Hi guys,

    I'm trying to find a way to cut off access to shared files at a specific time. I have some specific xlsm reports that are generated automatically at a specific hour with the same name but they cannot be generated due to users forggeting to close close excel.

    I have this script that is running as bat file from task scheduler at a specific time : $scan = net file
    $results = $scan | Select-String -SimpleMatch "D:\tests\reports\month"

    foreach ($result in $results) {
        #Get id
        $id = $result.Line.Split(" ")[0]

        #Close file
        net file $id /close

    It works on my servers with Windows server 2012 but not the ones with Windows server 2003.

    Any suggestions ? Been searching forums for a different version of a similar script but it doesn't work.


    Wednesday, May 10, 2017 9:48 AM

All replies

  • See:

    openfiles /?


    • Proposed as answer by jrv Wednesday, May 10, 2017 10:38 AM
    Wednesday, May 10, 2017 10:01 AM
  • See:

    openfiles /?


    What do you mean ? I'm not very good with powershell unfortunatelly :(
    Wednesday, May 10, 2017 10:10 AM
  • That is not PowerShell it is just a basic Windows command.


    Wednesday, May 10, 2017 10:13 AM
  • I'm running it from powershell and  i got if from here : https://social.technet.microsoft.com/Forums/Lync/en-US/e03753fa-3f93-4677-ae21-3480dce103b7/help-with-powershell-script-to-close-open-files?forum=winserverpowershell
    Wednesday, May 10, 2017 10:23 AM
  • I a sorry but we cannot fix files you have found on the Internet.  If you are not a tech then I recommend contacting a tech and have them help you understand what a Windows command is.

    The command will do what you ask.  Have you even tried to use it?


    Wednesday, May 10, 2017 10:38 AM
  • Do you really think i'd have access to the coorporate network to run scripts if i was just a random fella ? 

    It's ok, you can delete this, it was foolish of me to think i'd find help here.

    Wednesday, May 10, 2017 10:54 AM
  • You certainly don't understand how to type a simple command.  Try typing it at a prompt.  What do you get?

    Just because you can run scripts does not make you a trained technical professional and any tech knows how to run a command.

    Just open any command prompt (type <windows key - R> and type CMD.  When the command window opens type "openfiles /?".  You will see the instructions on how to use the command.  The rest of it is up to you.

    The link you posted clearly notes that the code proposed doesn't work.  You must disconnect the users session to keep the files closed.  That is what the "openfiles" command is for.


    • Edited by jrv Wednesday, May 10, 2017 11:04 AM
    Wednesday, May 10, 2017 11:01 AM
  • I'm running the command in windows server 2012 and it does what it's supposed to but doesn't work in windows server 2003.Tried to even include file name but then i get this "You cannot call a method on a null-valued expression.".. again, this works also in windows server 2012.

    I'm not sure why it's not working in both scenarios. I get what you are saying that you cannot fix codes from internet.. i'll keep digging for a solution. Thanks for the help.

    Wednesday, May 10, 2017 11:15 AM
  •  WS2003 is no longer supported.  You can get the file utilities from the Windows Resource Kit or from third parties.

    Again - closing the file without disconnecting the session with Excel will not close the file. It will just be reopened due to file handle caching.

    For  WS2003 see: http://windowsitpro.com/windows/jsi-tip-7298-windows-xp-and-windows-server-2003-openfiles-utility


    Wednesday, May 10, 2017 11:23 AM