Using Everything search command line (es.exe) via PowerShell

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

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
                                               }
        }
    }
}

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:

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' }
	)
}

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)

Advertisements

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s