Jump to content

Recommended Posts

Posted

Hello

 

Could someone help me with a Powershell script? I need to update/replace job titles in AD from a CSV. The format of the CSV is: username, jobtitle.

 

Thanks

Posted
Hello

 

Could someone help me with a Powershell script? I need to update/replace job titles in AD from a CSV. The format of the CSV is: username, jobtitle.

 

Thanks

 

Bit rough and ready but here it is, should work.

 

# Start of Script

 

Import-Module ActiveDirectory

Set-ExecutionPolicy -ExecutionPolicy Bypass

 

$rows = import-csv "IMPORT_FILE_PATH.csv"

 

 

# Uses Active Directory module for Windows Power Shell

ForEach ($row in $rows) {

 

$SamAccountName = $row.username

$title = $row.jobtitle

 

Set-ADUser -Identity $SamAccountName -Title $title

 

}

 

# End of Script

  • Thanks 1
Posted

hey techman, davit's script worked fine for me in a test enviro so should work fine.

 

Not wanting to state the obvious but have you changed the $rows = import-csv "IMPORT_FILE_PATH.csv" to point to the location of the csv? eg $rows = import-csv "c:\temp\staff.csv"

  • Thanks 1
Posted (edited)

As good as powershell is sometimes it can be like hitting a nail with a sledgehammer. I have a dsmod command script that does similar but changes the password but i have included the switch for the "office" field in AD (i assume this is the field you mean to change)

 

ChangeUserDetails.cmd


for /f "eol=; Tokens=1,2,3,4,5,6,7,8 Delims=," %%a in (%1) do (
dsmod user "CN=%%a,OU=removed,OU=removed,OU=removed,OU=removed,OU=removed,DC=removed,DC=removed,DC=removed,DC=removed,DC=removed" -upn %%a -display %%b -samid %%a -desc %%c -fn %%d -ln %%e -office %%g -pwd %%f -mustchpwd no
)
[/Code]

 

ChangeUserDetails.CSV format

[Code]
User,Display name,Description,First Name, Last Name, Password, Office
[/Code]

Adapt your csv and script to suit

in elevated cmd prompt

[Code]
ChangeUserDetails.cmd ChangeUserDetails.csv
[/Code]

 

EDIT: Subsititute the -office switch with -Title. didnt see the job title field first time i looked

Edited by dapaulio
Posted

Are you running this on the AD server or local machine. We run ours on an Exchange server but it works OK. We only do this as our create account script runs on the Exchange server and wanted to keep them together.

 

Just strange how you are not getting an error message TBH. Are you running RM or Vanilla network??

Posted

Did you know you can use short hand ForEach in ps so instead of:-

ForEach ($row in $rows) {

 

you can use the following:-

$rows | %{

 

Hope this helps (or at least saves you guys some key presses!)

Posted
I suppose if you don't code much and don't comment your code foreach would be easier. A bit like where-object and ? if you like typing you use the first if you code for a living you use the latter! :-P
  • Thanks 1
Posted
Did you know you can use short hand ForEach in ps so instead of:-

ForEach ($row in $rows) {

 

you can use the following:-

$rows | %{

 

Hope this helps (or at least saves you guys some key presses!)

 

Important note: best practice is to never use an alias in a script. It might save a few key presses but makes it less readable and assumes that anyone else who reads it will know what it means. Save the alias use for the command line where you are just wanting to get the results quickly.

Posted (edited)

Indeed you are correct best practice does say that code being shared in the code repository should not use the alias name to make them readable. But for internal use stuff that is well commented there is no issue with their use. I automate using PS for a living and churn out scripts and functions for various tasks daily just trying to share some PS Love.

 

oh this little tool can help

Edited by HPlum78
link
  • Thanks 1
Posted
Indeed you are correct best practice does say that code being shared in the code repository should not use the alias name to make them readable. But for internal use stuff that is well commented there is no issue with their use. I automate using PS for a living and churn out scripts and functions for various tasks daily just trying to share some PS Love.

 

oh this little tool can help

 

Agree, there are certainly a lot of code available but when it is un-commented and using aliases as a new comer it is quite difficult to learn what is doing what especially when some of the coding does not even work. At the end of the day probably shouldn't be running any old random untested code that you picked off the internet straight on wide scale on production servers anyway.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...