FeaturedUncategorized

Powershell | Search Directory for files matching CSV

I currently use Adaxes to manage all our AD/Exchange accounts and have a report built to find accounts missing staff photos. I’ve wanted to streamline this process as currently we have our front off staff take staff photos when they’re onboarded then dump the photos into a network share. In doing some account clean up I needed an easy way to search a network share for photos matching staff names in a CSV.

After some searching this is what I came up with and has been working well!

 $CSVFile = "C:\Temp\users without photos.csv"
 $SearchDir = "\\adss\_Exchange-Photos\"

 ForEach ($Name in (Import-Csv -Path $CSVFile )){
     # Search within path for files matching CSV filename pattern
     Get-Childitem -Path $SearchDir -File -Filter ($Name.Name + "*") | Foreach {
         #On each source jpg found in the folder, copy it into the folder which matches value from CSV
         Copy-Item $_.fullname -Destination ("C:\temp\" + $Name.filename)
         Write-Output "Found match and copied: $_"
     }
 }
My temp dir after running the script and finding a match
Powershell output
CSV Format
Show More

Cory Fiala

Technology and outdoor enthusiast. Currently the sole systems administrator for a local K-12 school district with 800+ staff and 5,500+ students. I support windows servers, Linux systems (FreeBSD and Ubuntu), along with vSphere (4 node cluster). 60+ virtual machines and 8 physical servers. This doesn't include my personal homelab.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button