Last active
August 6, 2020 16:04
-
-
Save vestjoe/fac1af367ae386ab8e820a56adc711b6 to your computer and use it in GitHub Desktop.
PowerShell file search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Find-Files() { | |
<# | |
.SYNOPSIS | |
Returns file list based on search terms. | |
.DESCRIPTION | |
Function: Find-Files | |
Author: Joe Vest, MINIS LLC | |
Date: 26 March 2015 | |
.PARAMETER searchBase | |
Specifies the file output directory path | |
.PARAMETER searchTerms | |
Specifies the file output directory path | |
.PARAMETER LogPath | |
Specifies the file output directory path | |
.EXAMPLE | |
Find-Files -searchBase "c:\users\me\desktop" -searchTerms "*.txt,*.docs,*.web.xml,*web.config*,*users.xml*,*password*,*.ps1" -LogPath "c:\users\me\desktop\test" | |
Example of reading output | |
Import-Clixml .\target_20190101_164525.xml | ft | |
#> | |
[CmdletBinding()] Param( | |
[Parameter(Mandatory=$True)] | |
[ValidateScript({If(Test-Path -Path $_){$true}else{Write-host "`n**************`nERROR: Invalid searchBase Path: $_" `n**************`n;exit}})] | |
[String] $searchBase, | |
[Parameter(Mandatory=$True)] | |
[String] $searchTerms, | |
[Parameter(Mandatory=$True)] | |
[ValidateScript({If(Test-Path -Path $_){$true}else{Write-host "`n**************`nERROR: Invalid LogPath: $_" `n**************`n;exit}})] | |
[String] $LogPath | |
) | |
# Enforce Powershell Version 2.0 | |
Set-StrictMode -Version 2.0 | |
# Ignore Errors and don't print to screen | |
$ErrorActionPreference = "SilentlyContinue" | |
# Create filename for enumeration output file | |
$Time = (Get-Date).ToUniversalTime() | |
[string]$StartTime = $Time|Get-Date -uformat %Y%m%d_%H%M%S | |
[string]$Hostname = [System.Net.Dns]::GetHostName() | |
[string]$FileName = $StartTime + '_' + $Hostname + '.xml' | |
[String] $LogFile = (Join-Path $LogPath $FileName) | |
write-host "************************************" | |
write-host "Find-Files" | |
write-host "------------------------------------" | |
write-host "Search Base : " $searchBase | |
write-host "Search Terms: " $searchTerms | |
write-host "------------------------------------" | |
# Start Search | |
write-host "Search Started..." | |
$search = (Get-ChildItem -Recurse -Include $searchTerms.replace(' ','').split(',') -Path $searchBase | Where-Object { ($_.PSIsContainer -eq $false) }) | |
$total = 0 | |
$total = $search.count | |
$search | Select-Object FullName,LastWriteTime,LastAccessTime | Export-Clixml $LogFile | |
write-host "Search Complete." | |
write-host "" | |
write-host $total "files found." | |
write-host "Search LogPath written to " $LogFile | |
write-host "************************************" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment