Close Open Files with Windows Powershell

Leverage HANDLE.EXE from Sysinternals to AUTOMATICALLY search and close files in Windows. Supports Piping

 
 
 
 
 
3.8 Star
(10)
Add to favorites
Storage
8/29/2013
E-mail Twitter del.icio.us Digg Facebook
  • Scheduled Task?
    2 Posts | Last post March 29, 2015
    • Is there a way to get this to run as a scheduled task?
      I can't seem to get it to run without running PoSh elevated (As Administrator)
      
      Thanks
    • Actually as a thought, try it scheduled as Network Service or SYSTEM.  Both should have the appropriate rights
  • It's a good starting point
    2 Posts | Last post August 29, 2013
    • I used this as a stating point and rewrote it somewhat. But some general comments.
      
      There's random casing used on functions, variables and keywords. I would clean this up :) Also, why pass string arrays to a function when there only is one value?
      
      Any one using this script should apply the fixes mentioned here, and then clean it up somewhat as well for readability.
    • Mikael
      
      Casing won't affect the operation of Functions in PowerShell, it's VISUALLY nicer (I 100% agree) but "Function"ally won't break.
      
      Cheers! 
      Sean
  • There is an error.
    2 Posts | Last post August 02, 2012
    • Parameter declarations are a comma-separated list of variable names with optional initializer expressions.
      At C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\closefiles\closefiles.psm1:36 char:17
      +                  <<<< )
          + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : InvalidFunctionParameter
       
      Import-Module : The specified module 'closefiles' was not loaded because no valid module file was found in any module directory.
      At line:1 char:14
      + import-module <<<<  -name closefiles
          + CategoryInfo          : ResourceUnavailable: (closefiles:String) [Import-Module], FileNotFoundException
          + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
    • Hi Hinker, there were a couple of things I did to get this working:
      
      1. I removed the second parameter from the GET-Openfile function, so the parameters for this function now look like:
      param
                      ( 
                      [parameter(Mandatory=$True,
                      ValueFromPipeline=$True)]
                      [String[]]$Filename
                      
                      )
      2. I removed the last comma in the parameters list for the close-openfilefunction, so the parameters now look like:
      Param(
                      [parameter(Mandatory=$True,
                                      ValueFromPipelineByPropertyName=$True)]
                                      [string[]]$ProcessPID,
                      [parameter(Mandatory=$True,
                                      ValueFromPipelinebyPropertyName=$True)]
                                      [string[]]$FileID,
                      [parameter(Mandatory=$false,
                                      ValueFromPipelinebyPropertyName=$True)]
                                      [String[]]$Filename
                      )
      3. I then saved the script down as a .psm1 file (I called it OpenFiles.psm1) and saved it in my modules folder (C:\windows\System32\WindowsPowerShell\v1.0\Modules). 
      4. Lastly, I ran the command import-module .\OpenFiles.psm1