help me creating scrip to rename files in a folder with name "extAttribute1" to "SAmaccountName"

Hello ,
I have a folder with .jpg files inside. The files are pictures of users  and are named using their correspondent "ext attribute 1" .  I have also exported to a csv file all users inside an OU with Name, SamAccountName, extAttribute1.  extAttribute1 has 7 digits.  
Now, I need to for all files in the folder that have a file name with 7 digits to rename it to it's correspondent SamAccountName . If file name exists with SamAccountName, overwrite it.
I have several powershell commands to obtain .csv with the files name and the other mentioned before with AD account information for users in OU.
Can someone help me to achieve this.
Ernesto GallardoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is in test mode and will only display what it would rename. Remove the -WhatIf in line 5 to run it for real.
$ht = @{}
Import-Csv -Path "C:\Temp\Users.csv" | ForEach-Object {$ht[$_.extAttribute1] = $_.SamAccountName}
Get-ChildItem -Path "C:\Folder\jpg"  -Filter *.jpg | Where-Object {$_.BaseName -match '\A\d{7}\Z'} | ForEach-Object {
	If ($ht.ContainsKey($_.BaseName)) {
		Rename-Item -Path $_.FullName -NewName $ht[$_.BaseName] -WhatIf
	} Else {
		Write-Warning "'$($_.Name)': no match in csv!"

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ernesto GallardoAuthor Commented:
Thanks for your quick response, I can't test it right now, but I will later on tonight (I'm on eastern time) I appreciate your help .
Ernesto GallardoAuthor Commented:
Perfect Solution, extremely fast.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.