Link to home
Start Free TrialLog in
Avatar of Doug Dohner
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\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...
Avatar of Jeremy Weisinger
Jeremy Weisinger

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.
Avatar of Doug Dohner

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.Number)" ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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!
Glad to help!