Disclaimer
This is my personal blog. The opinions and views I express are my own. The information I provide is on an as-is basis. I make no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this blog and will not be liable for any errors, omissions, or delays in this information or any losses, injuries or damages arising from its use.

Search file structure for a specific file type w/ PowerShell

This script will search a file structure for a specific file type and export the results to a CSV file. This can be used to search a user accessible file share for file types that you may have policies against storing on a file share. If you don’t have access to Enterprise tools like FSRM this is an easy way to provide similar functionality.

 

<#
*** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ***
.DESCRIPTION
    This script will search a file structure for a specific file type and export the results to a CSV file. 
    This can be used to search a user accessible file share for file types that you may have policies 
    against storing on a file share. If you don’t have access to Enterprise tools like FSRM this is an 
    easy way to provide similar functionality.

.NOTES
	File Name: do-search.ps1
	Author: David Hall
	Contact Info: 
		Website: www.signalwarrant.com
		Twitter: @signalwarrant
		Facebook: facebook.com/signalwarrant/
		Google +: plus.google.com/113307879414407675617
		YouTube Subscribe link: https://www.youtube.com/c/SignalWarrant1?sub_confirmation=1
	Requires:  
	Tested: Windows 10
 
.PARAMETER
 
.EXAMPLE
#>

#This is what Directory you want to search
$searchDir = "c:\files"

#This is what file or file type(s) you're searching for
$searchFile = "*.mp3"

#This is the location of your logfile of the results
$outputDir = "C:\scripts\files_$(get-date -f yyyy-MM-dd).csv"

$files = Get-ChildItem -Path $searchDir -Recurse -Filter $searchFile -EA silentlyContinue | 
    Select Fullname, @{Name="MegaBytes"; Expression={"{0:F2}" -f ($_.Length / 1MB)}}, CreationTime, LastAccessTime | 
    Sort-Object MegaBytes -Descending

#The results output to a CSV file
$files | Export-Csv $outputDir -Append

write-host -foregroundcolor yellow "Search complete, results can be viewed here: $outputDir"

#Start excel and open the logfile of the results
Start-Process Excel -ArgumentList $outputDir

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.