Get-OpenFiles.ps1
function Get-OpenFiles { <# .SYNOPSIS List open files on a remote computer. .DESCRIPTION Connects to a remote computer and list open files. You can also filter the list with a keyword. Wildcard is %. .PARAMETER ComputerName Remote computer hostname that you want to close files. .PARAMETER FilterKey Filter results with a keyword. Wildcard is %. .EXAMPLE List all opened files with a '.pdf' extension on a remote computer named 'server1'. PS C:\> Get-OpenFiles server1 "%.pdf" .NOTES Author: Yannick Gagne .LINK http://msdn.microsoft.com/en-us/library/ #> Param ( [string]$ComputerName, # 1st param is remote computer you want to list open files from [string]$FilterKey # 2nd param is a filter that's applied to the opened files path, wildcard is % ) # get list of open files with openfiles.exe, output it to CSV format $openfilescsv = openfiles.exe /query /s $computerName /fo csv /V # skip headers on first line $openfilescsv = $openfilescsv | select -skip 1 # convert CSV to objects $openfiles = ConvertFrom-Csv $openfilescsv -Header "computer","id","user","os","int","mode","fullname" # create datatable $dt = New-Object System.Data.DataTable("files") # table schema $cols = @("computer","id","user","os","int","mode","fullname") foreach ($col in $cols) { $dt.Columns.Add($col) | Out-Null } # add values to table foreach ($line in $openfiles) { $row = $dt.NewRow() foreach ($col in $cols) { $row[$col] = $line.$col } $dt.Rows.Add($row) | Out-Null } # create a dataview to filter data with $FilterKey & sort by paths $dv = New-Object System.Data.DataView($dt) $dv.RowFilter = "fullname like '$($FilterKey)'" $dv.Sort = "fullname" # create another view with only wanted columns $displayDt = $dv.ToTable($False, "fullname", "user", "id") $displayDv = New-Object System.Data.DataView($displayDt) # output results to console if ($displayDv.Count -gt 0) { $displayDv } else { Write-Host "No files found!" } } |