Skip to content

Instantly share code, notes, and snippets.

@pcast01
Created December 27, 2016 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcast01/1593a0f632fa36765a100f085ea81015 to your computer and use it in GitHub Desktop.
Save pcast01/1593a0f632fa36765a100f085ea81015 to your computer and use it in GitHub Desktop.
Function Format-FileSize() {
Param ([int]$size)
If ($size -gt 1TB) {[string]::Format("{0:0.00} TB", $size / 1TB)}
ElseIf ($size -gt 1GB) {[string]::Format("{0:0.00} GB", $size / 1GB)}
ElseIf ($size -gt 1MB) {[string]::Format("{0:0.00} MB", $size / 1MB)}
ElseIf ($size -gt 1KB) {[string]::Format("{0:0.00} kB", $size / 1KB)}
ElseIf ($size -gt 0) {[string]::Format("{0:0.00} B", $size)}
ElseIf ($size -eq 0) {"0 MB"}
Else {""}
}
$ver = $psversiontable.psversion
Write-Host "PowerShell version: $ver"
Write-Host "Script Mission: takes a list of locations to search and looks at another "
Write-Host " list that has specific words to match and compilates a list"
Write-Host " with information about matches found in the filename."
Write-Host "---------------------------------------------------------------------"
Write-Host " Select Folder where Locations and Patterns text files are located when prompted"
$startScript = Read-Host -Prompt 'Hit enter to continue'
$application = New-Object -ComObject Shell.Application
$path = ($application.BrowseForFolder(0, 'Select root folder of new WebSite', 0)).Self.Path
if([string]::IsNullOrEmpty($path))
{
Write-Host "Exiting script..."
exit
}
cd $path
$locPath = $path + "\locations.txt"
$patternPath = $path + "\patterns.txt"
if(![System.IO.File]::Exists($locPath)){
Write-Host "* Locations text file does not exist in $path. Exiting..." -ForegroundColor Red
Exit
}
if(![System.IO.File]::Exists($patternPath)){
Write-Host "* Patterns text file does not exist in $path. Exiting..." -ForegroundColor Red
Exit
}
$folders = gc locations.txt
$patterns = gc patterns.txt
foreach ($folder in $folders) {
$files = gci -Path $folder -Recurse *.*
Write-Host "Searching folder: $folder -------------------------"
$file = $file + "********************************************************************`r`nSearching folder: $folder`r`n********************************************************************`r`n"
foreach ($pattern in $patterns) {
$resultsFile = Get-ChildItem -Recurse -Force $folder -ErrorAction SilentlyContinue |
Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$pattern*") } |
Select-Object @{Name="Folder";Expression={$_.Directory}},@{Name="FileName";Expression={$_.Name}} ,
@{Name="Size";Expression={Format-FileSize($_.Length)}}, @{Name="Last Modified Date";Expression={$_.LastWriteTime}},
@{Name="Owner";Expression={(Get-acl $_.FullName).Owner}} | Format-Table -AutoSize * | Out-String -Width 4096
$rNumbers = Get-ChildItem -Recurse -Force $folder -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and ( $_.Name -like "*$pattern*") }
$resultsFile
$rCount = $rNumbers.Count
Write-Host "Count: $rCount"
$file = $file + "=========== Searching for $pattern ===========`r`n**Number of Files found: $rCount`r`n`r`n" + $resultsFile
}
}
$file | Out-File results.txt
Clear-Variable -name file
Clear-Variable -Name resultsFile
.\Results.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment