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

Posted on 2018-03-14
Low Priority
Last Modified: 2018-03-14
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.
Question by:Ernesto Gallardo
  • 2
LVL 86

Accepted Solution

oBdA earned 1000 total points
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


Author Comment

by:Ernesto Gallardo
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 .

Author Closing Comment

by:Ernesto Gallardo
Perfect Solution, extremely fast.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
The Windows Firewall provides an important layer of protection and a rich interface to configure it. Unfortunately, it lacks item level filtering. This article details my process of implementing firewall-as-code to reduce GPO bloat.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question