Solved

Powershell script to change an attribute for all active directory users

Posted on 2014-07-22
9
2,455 Views
Last Modified: 2014-07-26
I need to create a script that will rename all active directory users attritbute (cn). Just for clarification this is the the CN= of the DistinguishedName field ( I briefly mixed this up). Mainly, I need the command to change this field in AD.
0
Comment
Question by:markc56
  • 3
  • 3
  • 2
9 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40211957
You can use Rename-ADObject to change the name of the object (With windows 2008 R2 DC and above)..
Ref : http://ss64.com/ps/rename-adobject.html

If you can explain, what modification you need for the name then only we can suggest further.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40212105
cn, I believe it is stand for Contexts Name.

anyway what changes you would like to make by script?

like add some prefix or change it to match First Name Last Name?

would you please provide some detail regarding on it.

for instance,

you can do
example

get-aduser -id abc | set-aduser -replace @{cn="abc01"}
0
 
LVL 3

Author Comment

by:markc56
ID: 40212226
We have a secondary program for ticketing that we need to import users into from AD. In the cn field we not only have a name, but area of work and here is where the problem is. We are using a "/" between areas and the ticketing program will not recognize the "/", so we need to change it to a "-" or "_" which it does recognize.
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40212317
do you have last name, first name as the CN?
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 40212456
0
 
LVL 3

Author Comment

by:markc56
ID: 40212781
Justin: When I tried running the command you listed (get-aduser -id abc | set-aduser -replace @{cn="abc01"}) I found out you cannot make a change. It errors out referencing a system file error. Thanks for help.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 40213098
CN and name of the object will be same.. Following cod will work for you. It will replace all the "/" in name with "-"
Get-ADUser -Filter * | ?{$_.Name -match "/"} | % {
$User = $_
  $User | Rename-ADObject -Newname $($User.Name -replace "/","-")
}

Open in new window

If you want to run it against a OU the use -SearchBase parameter..
Get-ADUser -SearchBase "OU=Users,OU=HQ,DC=Max,DC=com" -Filter * | ?{$_.Name -match "/"} | % {

Open in new window

0
 
LVL 3

Author Closing Comment

by:markc56
ID: 40221728
This works exactly as I needed.  Thanks!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In-place Upgrading Dirsync to Azure AD Connect
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

713 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