Issue with AD attribute

Welcome Forums General PowerShell Q&A Issue with AD attribute

This topic contains 3 replies, has 3 voices, and was last updated by

Ben
 
Participant
1 year ago.

  • Author
    Posts
  • #88847
    Ben

    Participant
    Points: 2
    Rank: Member

    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

  • #88850
    Jon

    Participant
    Points: 38
    Rank: Member

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

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

    Participant
    Points: 0
    Rank: Member
    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.

  • #88873
    Ben

    Participant
    Points: 2
    Rank: Member

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

The topic ‘Issue with AD attribute’ is closed to new replies.