Powershell script to rename files with active directory info

Hi,

I am looking for some help writing a powershell script.
The problem is that I have several pictures in a folder that have a filename that contains employeeID.jpg (for example 1145712.jpg)
What I would like is that the filenames are renamed to username.jpg. The usernames should be fetched from active directory, where I also have the employeeID, so I can find the username.

In the end the output of the script should be
ren 1145712.jpg jdoe.jpg

When John Doe's username is jdoe and his employeeID is 1145712

The only thing I have come up so far is this code snippet:
$USERS = Get-Content c:\employeIDs.txt
$USERS|Foreach{Get-ADUser -f {employeeID -eq $_}  -Properties employeeID, samAccountName |Select employeeID, samAccountName} | ft -HideTableheaders

Some help would be greatly appreciated.
helhasAsked:
Who is Participating?
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.

oBdACommented:
Just set the path to the folder with the pictures. The script will find all jpg files with names consisting only numbers, retrieve the SamAccountName based on the file name, and rename the file.
It'll generate some console output while it's working, and then return an array of PSObjects with the results you can use for further processing (for example '.\whatever.ps1 | Export-Csv -Path C:\temp\rename.csv -NoTypeInformation')
PicturesFolder = "C:\Temp"
Import-Module ActiveDirectory
$ResultList = @()
Get-ChildItem -Path $PicturesFolder -Filter "*.jpg" |
	? {$_.BaseName -match '\A\d+\Z'} |
	% {
		$Result = $_ | Select-Object -Property `
			Directory,
			Name,
			NewName,
			Result
		"'$($_.Name)'" | Write-Host -NoNewline
		If ($ADUser = Get-ADUser -Filter "employeeID -eq '$($_.Basename)'") {
			$Result.NewName = $($ADUser.SamAccountName) + $_.Extension
			Rename-Item -Path $_.FullName -NewName $Result.NewName -Force -ErrorAction SilentlyContinue
			If ($?) {
				$Result.Result = "OK"
				" --> '$($Result.NewName)'" | Write-Host -ForegroundColor Green
			} Else {
				$Result.Result = "ERROR: $($Error[0].Exception.Message)"
				": $($Result.Result)" | Write-Host -ForegroundColor Red
			}
		} Else {
			$Result.Result = "ERROR: no such employeeID"
			": $($Result.Result)" | Write-Host -ForegroundColor Red
		}
		$ResultList += $Result
	}
$ResultList | Write-Output

Open in new window

0

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
helhasAuthor Commented:
Very nice script! Exactly what I needed, there was only one small error with PicturesFolder that was missing a $ sign in the first rule.
Many thanks oBdA!!!
0
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
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.