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 for specific file extension (Powershell)

*** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ***

This script will allow you to search a directory and all its subfolders for a file extension of your choosing. The output includes the file path, last write time and the size in MBs.

I have used this to search a share for old mp3 files or avi files in order to clean-up our SAN device. I suppose you could also sort it by the lastwritetime so if you had a file that was last written to in 1998 one would assuming it’s probably ok to delete it or archive it.

You can search the local (C:\), any mapped drive (Z:\) or use it to search a SAN or NAS using the UNC path (\\fileshare\share or \\192.168.1.1\share)

Tested on Windows Server 2K3, Windows Server 2K8, Windows Server 2K8 R2, Windows 7 x64, Windows Vista.

#  *** THIS SCRIPT IS PROVIDED WITHOUT WARRANTY, USE AT YOUR OWN RISK ***
#
# Author: David Hall, signalwarrant.com
#
# You can search the local (C:\), any mapped drive (Z:\) or use it to search a SAN or NAS using the 
# UNC path (\\fileshare\share or \\192.168.1.1\share)

$dir = read-host "Enter Directory"
$ext = read-host "Enter the File Extension"

gci "$dir" *$ext -r | Select-Object FullName,LastWriteTime, @{Name="MegaBytes"; Expression={"{0:F2}" -f ($_.Length / 1MB)}} | Sort-object MegaBytes -desc | export-csv c:\files.csv
Write-host -foregroundcolor Cyan "Search Complete"

 

Leave a Reply

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