FileFinder

Here’s a script playing around Get-ChildItem and Select-String.
Searches files from various criterias (before a modification date, after a modification date, with a minimum size, with a maximum size, by extension, by filename, by string contained in the file.) and generates an HTML report with the results.

<#
.SYNOPSIS
    FileFinder - Finds files with specified criterias.
.DESCRIPTION
    FileFinder - Finds files with specified criterias
    Searches files from various criterias and generates an HTML report with the results
.PARAMETER Mode
    Defines the criteria type for the search
    Mandatory parameter.
.PARAMETER Path
    Defines the root path for the search
    Default is "$pwd" (path from where you run the script).
.NOTES
    File Name  : FileFinder.ps1
    Author     : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
.EXAMPLE
    PS D:\>FileFinder.ps1 -Mode BeforeModificationDate
    The script will ask for a date, if 27/10/2012 is entered, the script will seach all files in "$pwd", recursively, modified before the 27/10/2012.
.EXAMPLE
    PS D:\>FileFinder.ps1 -Path D:\ -Mode Extension
    The script will ask for an extension, if ps1 is entered, the script will seach all files in "D:\", recursively, with an .ps1 extension.
#>
Param(
    [Parameter(Mandatory=$true, HelpMessage="You must provide a search mode.")]
    $Mode,
    $Path="$pwd"
    )

	$Report="$env:TEMP" + "\SearchResults.hta"
	#delete previous results file if exists
    if ((Get-Item $Report -ErrorAction "SilentlyContinue").Exists) {
        Write-Host "Deleting old $Report" -foregroundcolor Yellow
        Remove-Item $Report
    }
	$SearchDate=Get-Date -Format "dd/MM/yyyy HH:mm:ss"
	$global:ErrorActionPreference="SilentlyContinue"

switch ($Mode) 
    { 
	{$Mode -eq "BeforeModificationDate"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide the last modification date of the files you're looking for.`nAll files from `"$Path`" and modified " -NoNewline
	Write-Host "BEFORE" -ForegroundColor Yellow -NoNewline
	Write-Host " will be returned: " -NoNewline
	$Date=$input=Read-Host
	$Parameter=$Date
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Where-Object { ($_.LastWriteTime -le [DateTime]::parse("$Date")) -and (!$_.PSIsContainer)} | Sort-Object LastWriteTime | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="LastWriteTime";Expression={$_.LastWriteTime}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String	
	}
	{$Mode -eq "AfterModificationDate"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide the last modification date of the files you're looking for.`nAll files from `"$Path`" and modified " -NoNewline
	Write-Host "AFTER" -ForegroundColor Yellow -NoNewline
	Write-Host " will be returned: " -NoNewline
	$Date=$input=Read-Host
	$Parameter=$Date
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Where-Object { ($_.LastWriteTime -ge [DateTime]::parse("$Date")) -and (!$_.PSIsContainer)} | Sort-Object LastWriteTime | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="LastWriteTime";Expression={$_.LastWriteTime}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String	
	}
	{$Mode -eq "ByMinimumSize"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide the minimum size of the files you're looking for.`nAll files from `"$Path`" and " -NoNewline
	Write-Host "BIGGER THAN (MB)" -ForegroundColor Yellow -NoNewline
	Write-Host " specified size will be returned: " -NoNewline
	$Size=$input=Read-Host
	$Parameter=$Size
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Where-Object { (($_.Length / 1MB) -gt $Size) -and (!$_.PSIsContainer) } | Sort-Object Name,Size | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="Size (MB)";Expression={([math]::Round(($_.Length/1MB), 2))}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String
	}
	{$Mode -eq "ByMaximumSize"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide the maximum size of the files you're looking for.`nAll files from `"$Path`" and " -NoNewline
	Write-Host "SMALLER THAN (MB)" -ForegroundColor Yellow -NoNewline
	Write-Host " specified size will be returned: " -NoNewline
	$Size=$input=Read-Host
	$Parameter=$Size
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Where-Object { (($_.Length / 1MB) -lt $Size) -and (!$_.PSIsContainer) } | Sort-Object Name,Size | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="Size (MB)";Expression={([math]::Round(($_.Length/1MB), 2))}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String
	}
	{$Mode -eq "ByExtension"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide the extension of the files you're looking for.`nAll those files from `"$Path`" will be returned: " -NoNewline
	$Extension=$input=Read-Host
	$Parameter=$Extension
	$Results = @()
	Get-ChildItem -Path $Path -Recurse -Include "*.$Extension" | Where-Object { (!$_.PSIsContainer) } | Sort-Object Name | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String
	}
	{$Mode -eq "ByNamePattern"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide a part of the name for the files you're looking for.`nAll files from `"$Path`" and " -NoNewline
	Write-Host "CONTAINING THE PATTERN IN FILE NAME" -ForegroundColor Yellow -NoNewline
	Write-Host " will be returned: " -NoNewline
	$Pattern=$input=Read-Host
	$Parameter=$Pattern
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Where-Object { ($_.Name -like "*$Pattern*") -and (!$_.PSIsContainer)} | Sort-Object Name | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="Path";Expression={$_.Directory}})}
	$Results | Format-Table -AutoSize | Out-String
	}
	{$Mode -eq "ByContent"} {
	Write-Host "Info: The search mode is $Mode.`n" -ForegroundColor Yellow;
	Write-Host "Please provide a string to find in the files  you're looking for.`nAll files from `"$Path`" and " -NoNewline
	Write-Host "CONTAINING THE STRING" -ForegroundColor Yellow -NoNewline
	Write-Host " will be returned: " -NoNewline
	$String=$input=Read-Host
	$Parameter=$String
	$Results = @()
	Get-ChildItem -Path $Path -Recurse | Select-String -Pattern "$String" | Group-Object Path | % {
	$Results += @($_ | Select @{Name="FileName";Expression={$_.Name}},
	@{Name="Count";Expression={$_.Count}})}
	$Results | Format-Table -AutoSize | Out-String
	}
    }
	
	$Style=@"
	<title>Search Results</title> 
	<style type="text/css">
	h1 {margin: 0; padding: 0; position: relative; background: #104E8B;color: #FFF;}
	body {font: 12px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;}
	table{border: 1px dotted #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; width:100%;}
	th{background-color: #104E8B; color: #FFF; font-weight: bold; text-align:left; vertical-align:top;}
	td{border: 1px dotted #CCC; text-align:left; vertical-align:top;}
	#page-wrap { width: 50%; margin: 100px auto; }
	#bottom {clear:both; text-align:right;}
	</style>
"@

	$Body=@"
	<h1>&nbsp;Search Results</h1><br />
	<ul>
	<li>Search Mode: `"$Mode`" with parameter `"$Parameter`"</li>
	<li>Search Date: $SearchDate</li>
	<li>Path from which the search was initiated: `"$Path`"</li>
	</ul>
"@

	$Bottom=@"
	<div id="bottom">&copy; FileFinder.ps1 - 2012</div>
"@
	
	$Results | ConvertTo-HTML -Head $Style -Body $Body -PostContent $Bottom | Out-File $Report
	Write-Host "Generating the report`n" -ForegroundColor DarkGreen;
	Start-Sleep 2
	Invoke-Expression $Report

Leave a Reply

Scroll to Top