Renaming Multiple .JPG files with user's SamAccountName - PowerShell

Doug Dohner
Doug Dohner used Ask the Experts™
on
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\UserRename.csv" -Header "DisplayName","Number"

ForEach ($User in $ListofUsers) {
#Get username
write-host $User.DisplayName,$user.Number
$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...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
I think this is what you're looking for:
# Reads csv file
$ListofUsers = Import-Csv -Path "...EmployeePhotos\Testing\UserRename.csv" -Header "DisplayName","Number"
$JPGFolderPath = "...EmployeePhotos\Testing"

ForEach ($User in $ListofUsers) {
    #Get username
    write-host $User.DisplayName,$user.Number
    $userDname = $User.DisplayName 
    $sam = (Get-ADUser -Filter {Name -like $userDname} -Properties SamAccountName).SamAccountname 
    #Rename File
    if ($sam -ne $null) {
        Get-ChildItem -Path $JPGFolderPath -Filter $($user.number +".jpg") | %{Rename-Item $_ -NewName $_.Name.Replace("$($User.Number)","$sam")}
        } else {
            write-host "Bad Name $userDname" -ForegroundColor Red
        }
    }

Open in new window


Please test this as I have not. Let me know if you have any questions.

Author

Commented:
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.Number)" ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
Senior Network Consultant / Engineer
Commented:
Ah, try this:

# Reads csv file
$ListofUsers = Import-Csv -Path "...EmployeePhotos\Testing\UserRename.csv" -Header "DisplayName","Number"
$JPGFolderPath = "D:\Test"

ForEach ($User in $ListofUsers) {
    #Get username
    write-host $User.DisplayName,$user.Number
    $userDname = $User.DisplayName 
    $sam = (Get-ADUser -Filter {Name -like $userDname} -Properties SamAccountName).SamAccountname 
    #Rename File
    if ($sam -ne $null) {
        Get-ChildItem -Path $JPGFolderPath -Filter $($user.number +".jpg") | %{Rename-Item $_.FullName -NewName $_.Name.Replace("$($User.Number)","$sam")}
        } else {
            write-host "Bad Name $userDname" -ForegroundColor Red
        }
    }

Open in new window

Author

Commented:
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!
Jeremy WeisingerSenior Network Consultant / Engineer

Commented:
Glad to help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial