2 min read

Categories

Tags

tree8

Everything by voidtools is a great search utility for Windows. It returns almost instantaneous results for file and folder searches by utilizing the Master File Table(s). There is also a command-line version of everything (es.exe) and this post is about a wrapper I wrote in PowerShell around es.exe. The full version including full help (which I’m skipping here to keep it shorter) can be downloaded from my GitHub repository [code language=”powershell”] function Get-ESSearchResult { [CmdletBinding()] [Alias(“search”)] Param ( #searchterm [Parameter(Mandatory=$true, Position=0)] $SearchTerm, #openitem [switch]$OpenItem, [switch]$CopyFullPath, [switch]$OpenFolder, [switch]$AsObject ) $esPath = ‘C:\Program Files*\es\es.exe’ if (!(Test-Path (Resolve-Path $esPath).Path)){ Write-Warning “Everything commandline es.exe could not be found on the system please download and install via http://www.voidtools.com/es.zip” exit } $result = & (Resolve-Path $esPath).Path $SearchTerm if($result.Count -gt 1){ $result = $result | Out-GridView -PassThru } foreach($record in $result){ switch ($PSBoundParameters){ { $.ContainsKey(“CopyFullPath”) } { $record | clip } { $.ContainsKey(“OpenItem”) } { if (Test-Path $record -PathType Leaf) { & “$record” } } { $.ContainsKey(“OpenFolder”) } { & “explorer.exe” /select,”$(Split-Path $record)” } { $.ContainsKey(“AsObject”) } { $record | Get-ItemProperty } default { $record | Get-ItemProperty | select Name,DirectoryName,@{Name=”Size”;Expression={$.Length | Get-FileSize }},LastWriteTime } } } } [/code] The function contains a call to “Get-FileSize” a helper filter in order to return the file size of the selected items in proper format: [code language=”powershell”] filter Get-FileSize { “{0:N2} {1}” -f $( if ($ -lt 1kb) { $, ‘Bytes’ } elseif ($ -lt 1mb) { ($/1kb), ‘KB’ } elseif ($ -lt 1gb) { ($/1mb), ‘MB’ } elseif ($ -lt 1tb) { ($/1gb), ‘GB’ } elseif ($ -lt 1pb) { ($/1tb), ‘TB’ } else { ($/1pb), ‘PB’ } ) } [/code]

How does it work? The Get-ESSearchResult function (alias search) searches for all items containing the search term (SearchTerm parameter is the only mandatory parameter). The search results (if multiple) are piped to Out-GridView with the -PassThru option enabled so that the result can be seen in GUI and one or multiple items from within the search results can be selected. By default (no switches turned on) the selected item(s) are converted to FileSystemInfo objects and their Name, DirectoryName, FileSize and LastModifiedDate are output. The resulting objects can be used for further processing (copying, deleting….).

The switch Parameters add the following features and can be used in any combination:

  • -OpenItem : Invoke the selected item(s) (only applies to files not folders)

  • -CopyFullPath : Copy the full Path of the selected item to the clipboard

  • -OpenFolder : Opens the folder(s) that contain(s) the selected item(s) in windows explorer

  • -AsObject : Similar to default output but the full FileSystemInfo objects related to the selected item(s) are output

I hope that the function can also help some of you to find your files and folders faster from the commandline. I’ve written another blog post in relation to Everything and PowerShell: Search fiel content by keyword using Everyting + PowerShell + GUI

shareThoughts


photo credit: 983 Foggy Day via photopin (license)