Description

With HANDLE.EXE downloaded from www.sysinternals.com you can take this script and add it to your module to enable two new Cmdlets

GET-Openfile and CLOSE-Openfile

HANDLE.EXE must exist in a folder called C:\Handle *OR* edit the Variable $HANDLEAPP to the correct path in the module

Usage is

GET-Openfile DOC

Will return all open DOC files on the computer as an Object

XLS’ | GET-Openfile

Will search for all XLS files

GET-Openfile DOCX | Close-Openfile

Find all open instances of DOCX files and forcefully close

GET-Openfile PS1 | Close-Openfile –whatif

Do a “whatif” which is show what the Cmdlet WOULD do if you
DIDN’T type the “–whatif” Parameter (One of the greatest features of Windows
Powershell)

Enjoy, I did :)

Script

PowerShell
Edit|Remove
Function global:GET-OpenFilePID() 
                { 
                                param 
                                ( 
                                [parameter(ValueFromPipeline=$true, 
                                                Mandatory=$true)] 
                                [String[]]$HandleData 
                                ) 
                                 
                                Process 
                                { 
                                                $OpenFile=New-Object PSObject -Property @{FILENAME='';ProcessPID='';FILEID=''} 
                                                 
                                                $StartPid=($HandleData[0] | SELECT-STRING 'pid:').matches[0].Index 
                                                $OpenFile.Processpid=$HandleData[0].substring($StartPid+5,7).trim() 
                                                 
                                                $StartFileID=($HandleData[0] | SELECT-STRING 'type: File').matches[0].Index 
                                                $OpenFile.fileid=$HandleData[0].substring($StartFileID+10,14).trim() 
                                                 
                                                $OpenFile.Filename=$HandleData[0].substring($StartFileID+26).trim() 
                                                Return $OpenFile 
                                } 
                } 
                 
Function global:GET-Openfile() 
{ 
[Cmdletbinding()] 
param 
                (  
                [parameter(Mandatory=$True, 
                ValueFromPipeline=$True)] 
                [String[]]$Filename 
                 
                ) 
                 
                Process 
                { 
                If ( ! (TEST-LocalAdmin) ) { Write-Host 'Need to RUN AS ADMINISTRATOR first'Return 1 } 
                IF ( ! ($Filename) ) { Write-Host 'No Filename or Search Parameter supplied.' } 
                $HANDLEAPP="& 'C:\Handle\handle.exe'" 
                $Expression=$HANDLEAPP+' '+$Filename 
                 
                $OPENFILES=(INVOKE-EXPRESSION $Expression-like '*pid:*' 
                 
                $Results=($OPENFILES | GET-openfilepid) 
 
                Return $results 
                } 
} 
 
Function global:Close-Openfile() 
{ 
[CmdletBinding(SupportsShouldProcess=$true)] 
Param( 
                [parameter(Mandatory=$True, 
                                ValueFromPipelineByPropertyName=$True)] 
                                [string[]]$ProcessPID, 
                [parameter(Mandatory=$True, 
                                ValueFromPipelinebyPropertyName=$True)] 
                                [string[]]$FileID, 
                [parameter(Mandatory=$false, 
                                ValueFromPipelinebyPropertyName=$True)] 
                                [String[]]$Filename 
                ) 
                 
                Process 
                { 
        $HANDLEAPP="& 'C:\Handle\handle.exe'"                 
        $Expression=$HANDLEAPP+' -p '+$ProcessPID[0]+' -c '+$FileID[0]+' -y' 
                if ( $PSCmdlet.ShouldProcess($Filename) )  
                                { 
                                INVOKE-EXPRESSION $Expression | OUT-NULL 
                                If ( ! $LastexitCode ) { Write-host 'Successfully closed'} 
                                } 
                } 
} 
 
Function global:TEST-LocalAdmin() 
                { 
                Return ([security.principal.windowsprincipal] [security.principal.windowsidentity]::GetCurrent()).isinrole([Security.Principal.WindowsBuiltInRole] "Administrator") 
                }