Set Job Title Using PowerShell

Anonymous
2021-12-08T19:36:54.053+00:00

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@tiedtlaw 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 for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-12-08T19:51:20.353+00:00

    Hi @Anonymous ,

    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


2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-12-08T21:19:30.83+00:00

    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.


  2. Naveen Kumar 21 Reputation points
    2022-04-22T21:27:34.783+00:00

    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.