Author Posts

December 12, 2017 at 1:11 pm

Hi Everyone,

I am having a problem with the following script.

$users = import-csv C:\temp\titles.csv

foreach ($u in $users) {

set-aduser -identity $u.samaccountname -replace @{title= "$u.title"}

}

The results in the title attribute comes up as @{SAMACCOUNTNAME=fname.lname; Title=title}.title

The csv file only has the samaccountname and title as I thought I would make things easier for me to write up in this script but I do not know what I have written wrong under set-aduser for it to come up with the extra text.

Thanks in advance

Ben

December 12, 2017 at 1:21 pm

Try this, assuming your csv has headers of samaccountname, title

import-csv C:\temp\titles.csv | % {set-aduser -identity $_.samaccountname -title $_.title}

December 12, 2017 at 2:07 pm

set-aduser -identity $u.samaccountname -replace @{title= "$u.title"}

Putting quotes around $u.title is causing it to be be expanded to $u + '.title'. $u is an object, so it's showing the whole object and then appending '.title'. I think you can just remove the double quotes in this case, but if you need to do something like this in other cases, you need to force it to evaluate inside of the double quotes, like this.

set-aduser -identity $u.samaccountname -replace @{title= "$($u.title)"}

I tend to avoid the whole confusing order of operations by building a temp string and not allowing PS to guess at what I wanted it to do.

December 12, 2017 at 3:35 pm

Many thanks Jon and Ron for providing the feedback. I can confirm it is all working now. All because I misplaced the ""