question

JesusLopezMancilla-2223 avatar image
0 Votes"
JesusLopezMancilla-2223 asked NaveenKumar-9435 commented

Set Job Title Using PowerShell

I'm trying to write a PS script to update the job title for users in AD. I was given a CSV file with a list of users email address and I'm using that.

This is what my CSV file looks like:

"email","jobtitle"

"user@email.com"," A JOB"



This is what my script looks like.

Import-Module ActiveDirectory
Import-CSV -Path "pathtofile.csv" | Foreach-Object {
# properties from the csv
$mail = $.email
$title = $
.jobtitle
Get-ADUser -Filter {(mail -eq "$mail")} | Set-ADUser -Title $title
}




After running the scrip, PS does not return feedback and the AD attribute does not update for job title. Anyone know what I'm missing?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered NaveenKumar-9435 commented

Hi @JesusLopezMancilla-2223 ,

first of all: Please use the Code Sample option (the icon with the 101010) to post code.
The editor of Q&A is screwing up the code if posted as normal text.

Please try this:

 Import-Module ActiveDirectory
 Import-CSV -Path "C:\Users\jlopez\OneDrive - APC\Desktop\Icons\mngimport\10.29.2021- APC Corp List.csv" | Foreach-Object {
 # properties from the csv
 $mail = $_.email
 $title = $_.jobtitle
 $mail
 $title
 Get-ADUser -Filter "mail -eq '$mail'" -Properties * | Set-ADUser -Title $title
 }

$mail and $title should show the values of the csv line.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AndreasBaumgarten,

This worked! Thank you so much.
For future reference what was I missing?

I was reading the MS KB article for Set-ADUser and though that I had to use the ldapDisplayName?

0 Votes 0 ·

The most likely problem is the incorrect filter string. If the filter doesn't find the user then the $null return value won't pass through the pipe and the Set-ADUser never runs.

The only time you need the ldapDisplayName is if you use the -Add, -Remove, or -Replace parameter to work on a property that isn't handled by one of the other parameter names (like "-Title" in this case).


1 Vote 1 ·

Hi @JesusLopezMancilla-2223 ,

did the answer work for you? Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

1 Vote 1 ·

This worked. Answer accepted.

0 Votes 0 ·

From samaccountname and i need to update these 4 attributes: EmployeeID, Title, Manager, Department

I have a CSV file (c:\temp\adinfo.csv) with fields:
samaccountname, EmployeeID, Title, Manager, Department

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered NaveenKumar-9435 commented

Maybe something like this (with a little error checking thrown in)?

 Import-Csv -Path "pathtofile.csv" | 
     ForEach-Object {
         # properties from the csv
         if ($_.email.Trim().Length -gt 3){
             $mail = $_.email
             $u = Get-ADUser -Filter "mail -eq '$mail'"
             if ($u){
                 $u | Set-ADUser -Title $_.title
             }
             else{
                 Write-Host "User with e-mail address $mail was not found." -ForegroundColor Yellow
             }
         }
         else{
             Write-Host "E-Mail address is either empty or too short: '$($_.email)'"
         }
     }

Note: The Get-ADUser -Filter parameter takes a string value, not a script block.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is this mean that we can't update these 4 attributes: EmployeeID, Title, Manager, Department in one script? Please help.


I need to update all below mentioned fields, please let me know if i can update these in one script or do i need multiple scripts? Please help me with detailed steps, i am not so good in scripting. Thanks.

emailaddress
officephone
manager
Title
EmployeeID
Office
Department
Company
description should be same as Title (jobtitle)

0 Votes 0 ·

No, it doesn't mean that at all.

If you have a question, please create your own post. This thread already has an accepted answer.

Be sure and supply a sample data set, and the script you're having a problem with, or the script you've created so far.

0 Votes 0 ·

Thanks for your reply.
I have posted new question, please reply on that one, i need some kind of automation very badlay, i got some deadlines to do this task. Thanks.

0 Votes 0 ·
NaveenKumar-9435 avatar image
0 Votes"
NaveenKumar-9435 answered RichMatheisen-8856 commented

From samaccountname and i need to update these 4 attributes: EmployeeID, Title, Manager, Department

I have a CSV file (c:\temp\adinfo.csv) with fields:
samaccountname, EmployeeID, Title, Manager, Department

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This question has already been answered. If you need help with your (different) problem please post a new question

0 Votes 0 ·