0

I would like to change 150 employees their job title.

I have a csvfile called Titletest.csv with columns UserPrincipalName [the user.name under it] and Title [job title under it]

The PowerShell script:

Import-Module ActiveDirectory  

$users = Import-Csv -Path c:\scripts\Titlestest.csv | Foreach-Object {

$user = $_.user

$title = $_.title

#Selects the specified user and sets Job Title

Get-ADUser -Filter {(UserPrincipalName -eq $user)} | Set-ADUser -Title $title

}

I get errors saying:

Get-ADUser : Variable: 'user' found in expression: $user is not defined.
At line:14 char:1
+ Get-ADUser -Filter {(UserPrincipalName -eq $user)} | Set-ADUser -Titl ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Can someone please advise?

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mia Smith
  • 1
  • 1
  • For this to work properly, your CSV file will need to contain header columns, `user` and `title`, with the appropriate data in them. If your column header is `UserPrincipalName`, then you will need to change to `$user = $_.UserPrincipalName`. – AdminOfThings Mar 15 '20 at 14:16
  • As an aside: While seductively convenient, it's best to [avoid the use of script blocks (`{ ... }`) as `-Filter` arguments](https://stackoverflow.com/a/44184818/45375). – mklement0 Mar 15 '20 at 19:04

1 Answers1

1

The reason for your error is because $user has no assignment. You are attempting to assign $user the value of a property that does not exist. The header user apparently does not exist in your CSV file. See below for how to convert a csv into PowerShell objects and access their properties.

# Sample CSV TitleTest.csv
UserPrincipalName,Title
covid19@domain.com,Usurper
jsmith@domain.com,CEO
bossman@domain.com,CFO

Import-Csv -Path c:\scripts\TitleTest.csv | Foreach-Object {
    $user = $_.UserPrincipalName
    $title = $_.Title
    Get-ADUser -Filter 'UserPrincipalName -eq $user' | Set-ADUser -Title $title
}

Explanation:

When using Import-Csv on a proper CSV file, the first row of delimited data will be converted to the properties of all input objects. All succeeding rows will be converted to individual objects with the header properties and output as a collection (array) of those objects. If the -Header parameter is used, then values passed into the parameter will become the properties of the objects. It is important to have the same number of delimited items on each row to ensure proper mapping.

Once you are dealing with objects, you can access their property values using the member access operator .. The syntax is object.property. So since you have headers UserPrincipalName and Title, you will need to use $_.UserPrincipalName and $_.Title to access the associated values.

$_ is the current pipeline object within your Foreach-Object {} script block.

Note that you don't technically need to define $user and $title here. You can just access the properties directly from the current object:

Import-Csv -Path c:\scripts\TitleTest.csv | Foreach-Object {
    Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" |
        Set-ADUser -Title $_.Title
}
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
  • Hi, thank you for the feedback. I just tried that but I still get error: Get-ADUser : the search filter cannot be recognized: At line:10 char:5 Get-ADIser -Filter "UserPricipalName -eq ..." not sure what am I doing wrong.. my script: Import-Csv -Path C:\scripts\Titlestest.csv | Foreach-Object { Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'" | Set-ADUser -Title $_.Title } – Mia Smith Mar 15 '20 at 15:30
  • Does your CSV file have commas separating the fields? Inside of your `foreach-object` you could simply output `$_.UserPrincipalName` and `$_.Title` to see if you are returning any data. – AdminOfThings Mar 15 '20 at 17:15