Find that asset - PowerShell Script - Windows

Managing media assets is great nowdays, all these webUI CMS type applications, however, when trouble shooting a backend system I often find myself needing to locate specific files in a number of locations quickly. A collegue of mine came up with a nice little .bat script to search for a file name in a bunch of different server locations. However it was all hard coded and didn't format nicely on the screen.

I decided to pimp it up with a nice powershell script.....

Essentially at the command prompt use the format

./find_files_in_broadcast_servers.ps1 -f "yourfilename_or_wilcard" e.g. ./find_files_in_broadcast_servers.ps1 -f "catplayingpiano.mpg" ./find_files_in_broadcast_servers.ps1 -f "*cat*"

 

 

# Script to search files in locations listed in locations.txt #
# Usage - ./find_files_in_broadcast_servers.ps1 -f "yourfilename_or_wildcard" #
# Grab command arguments from command line - filename #

param([string]$f = "filename")

# Open locations.txt into $a for each search location (one per line in txt file with CRLF at end #

$a = Get-Content "C:\scripts\powershell\locations.txt"

# Use foreach loop to step through locations in locations.txt #

foreach ($i in $a)

    # Print seach location to console window in green #

    {Write-Host -ForeGroundColor green $i

    # Search for file, recurse subdirectorys with the root location specified in locations.txt, format in table, sort by name #

    dir $i -recurse -filter $f | sort name | ft directory, name, LastWriteTime}

# END #

You will need to create a locations.txt file in the location specified on the Get-Content line with all of your locations specifed. Use the windows UNC format. For example.... locations.txt..... \\server1\location1 \\server2\location2 \\server3\location3 \\server4\location4

 

Tip: you may need to set PowerShell security to run unsigned scripts, there are various different options dependant on your security needs.

See

PowerShell Security

 

Neil (themoog)