Instantly share code, notes, and snippets.

What would you like to do?
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
Write-Host "Exiting script..."
cd $path
$locPath = $path + "\locations.txt"
$patternPath = $path + "\patterns.txt"
Write-Host "* Locations text file does not exist in $path. Exiting..." -ForegroundColor Red
Write-Host "* Patterns text file does not exist in $path. Exiting..." -ForegroundColor Red
$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*") }
$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment