Doug Dohner
asked on
Renaming Multiple .JPG files with user's SamAccountName - PowerShell
I was hoping to get some help on writing a PowerShell script. The main goal of the script is to rename many JPG files within a folder. Currently, All of the JPGs are in sequential order ( 1.jpg, 2.jpg, 3.jpg....), and we need to rename those files with the username that is associated with the user who is in the photo, while still keeping the .jpg extension. We currently have an Excel file (CSV) that is two columns, one being "DisplayName" and the other is "Number" (the file name/number that is associated with the user). Our script currently reads:
# Reads csv file
$ListofUsers = Import-Csv -Path "...EmployeePhotos\Testing \UserRenam e.csv" -Header "DisplayName","Number"
ForEach ($User in $ListofUsers) {
#Get username
write-host $User.DisplayName,$user.Nu mber
$userDname = $User.DisplayName
$sam = Get-ADUser -Filter {Name -like $userDname} -Properties SamAccountName
#Rename File
Format-Table -HideTableHeaders
if ($sam.Samaccountname -ne $null) {
write-host $sam.Samaccountname
} else {
write-host "Bad Name $userDname" -ForegroundColor Red
}
}
$HashTable = @{a = 1 ;b = 2}
$var.GetEnumerator() | Get-Member -HideHeaders
foreach ($userDname in $ListofUsers) {
$HashTable = @{
$User.DisplayName = $sam
$ListofUsers = $User.Number
}
$HashTable
}
# You need to make it recognize that file 1 is attached to XXXXXXX file 2 is attached to XXXXX...ext.
#user.number =
#the get-children is looking at your C: NOT the actual folder
foreach ($file in $folder) {
Get-ChildItem -Path "...EmployeePhotos\Testing " -Filter *.jpg | Where-Object { $_user.number + $file.name + $_.extension }
}
As you can see our script goes to AD and pulls their SamAccountName, however, we are unsure as to how to replace the current file names with the users SAM...
# Reads csv file
$ListofUsers = Import-Csv -Path "...EmployeePhotos\Testing
ForEach ($User in $ListofUsers) {
#Get username
write-host $User.DisplayName,$user.Nu
$userDname = $User.DisplayName
$sam = Get-ADUser -Filter {Name -like $userDname} -Properties SamAccountName
#Rename File
Format-Table -HideTableHeaders
if ($sam.Samaccountname -ne $null) {
write-host $sam.Samaccountname
} else {
write-host "Bad Name $userDname" -ForegroundColor Red
}
}
$HashTable = @{a = 1 ;b = 2}
$var.GetEnumerator() | Get-Member -HideHeaders
foreach ($userDname in $ListofUsers) {
$HashTable = @{
$User.DisplayName = $sam
$ListofUsers = $User.Number
}
$HashTable
}
# You need to make it recognize that file 1 is attached to XXXXXXX file 2 is attached to XXXXX...ext.
#user.number =
#the get-children is looking at your C: NOT the actual folder
foreach ($file in $folder) {
Get-ChildItem -Path "...EmployeePhotos\Testing
}
As you can see our script goes to AD and pulls their SamAccountName, however, we are unsure as to how to replace the current file names with the users SAM...
ASKER
I received an error message saying that "1.jpg" and so on do not exist. I double and triple checked the file paths for both the csv and the $JPGFolderPath and they seem correct. Any suggestions?
ERROR:
Rename-Item : Cannot rename because item at '5.jpg' does not exist.
At line:12 char:80
+ ... ".jpg") | %{Rename-Item $_ -NewName $_.Name.Replace("$($User.N umber)" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationExceptio n
+ FullyQualifiedErrorId : InvalidOperation,Microsoft .PowerShel l.Commands .RenameIte mCommand
ERROR:
Rename-Item : Cannot rename because item at '5.jpg' does not exist.
At line:12 char:80
+ ... ".jpg") | %{Rename-Item $_ -NewName $_.Name.Replace("$($User.N
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationExceptio
+ FullyQualifiedErrorId : InvalidOperation,Microsoft
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Very quick, responsive, and intuitive! I've been stuck on this portion of the project for some time, and this was a huge relief.
Thanks, Jeremy!
Thanks, Jeremy!
Glad to help!
Open in new window
Please test this as I have not. Let me know if you have any questions.