question

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

Update AD attributes from a csv file

I need to update all below mentioned AD attributes from a CSV file, 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.

CSV file have filled:
Samaccountname,emailaddress,officephone,manager,Title,EmployeeID,Office,Department,Company

emailaddress
officephone
manager
Title
EmployeeID
Office
Department
Company

windows-server-powershellwindows-active-directory
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.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered NaveenKumar-9435 commented

You could continue adding the individual parameters and values to the Set-ADUser cmdlet, making it longer and more cumbersome to deal with, of you could use a hash to assemble the parameters and values and use "splatting" to keep the clutter down.

I also added some minimal error handling.

 Import-Csv -Path "C:\temp\adinfo.csv" | 
     ForEach-Object {
         # properties from the csv
         $acct = $samaccountname     # needed for error message
         $props = @{
             EmployeeID   = $_.EmployeeID
             Office       = $_.Office
             title        = $_.title
             Department   = $_.Department
             emailaddress = $_.emailaddress
             officephone  = $_.officephone
             manager      = $_.manager
             company      = $_.company
    
         }
         Try {
             Get-ADUser -Identity $_.SamAccountName -Properties * -ErrorAction STOP| 
                 Set-ADUser @props -ErrorAction STOP
             }
             Catch {
                 Write-Host "User '$acct' not found or failed to update: "
                 Write-Host $_
             }
         }
· 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.

Thanks for the command, it worked for me.

I need to set description same as job title, could you please add into this command

0 Votes 0 ·

Just replace line 14 (it's empty) with:

 description = $_.title

I think you should also download this free PDF: Windows-PowerShell-4

Read the 1st half of the book and do the exercises. When you feel confident, read the 2nd half of the book which demonstrates using PowerShell to perform common administrative tasks. The 2nd half uses what you learned in the 1st half.


0 Votes 0 ·

Thanks for your quick response and valuable help.

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

Every one of those attributes have a corresponding parameter on the Set-ADUser cmdlet. You can do what you need in a few lines of code.

· 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.

I have a CSV file (c:\temp\adinfo.csv) with fields:
Samaccountname,emailaddress,officephone,manager,Title,EmployeeID,Office,Department,Company

Import-Module ActiveDirectory
Import-CSV -Path "C:\temp\adinfo.csv" | Foreach-Object {
# properties from the csv
$SamAccountName = $.SamAccountName
$EmployeeID = $
.EmployeeID
$Office = $.Office
$title = $
.title
$Department = $_.Department
$SamAccountName
$EmployeeID
$Office
$title
$Department
Get-ADUser -Filter "SamAccountName -eq '$SamAccountName'" -Properties * | Set-ADUser -Title $title -EmployeeID $EmployeeID -Office $Office -Department $Department
}

I have this command which works for me to update only 4 attributes (Title,EmployeeID,Office,Department)
Could you please let me know what I need to add into above command to update emailaddress,officephone,manager, company attributes as well.

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @NaveenKumar-9435 ,

for the Set-AdUser command you will find the parameters and examples here: https://docs.microsoft.com/en-us/powershell/module/activedirectory/set-aduser?view=windowsserver2022-ps

Here you will find some examples how to work with Set-AdUser and how to update multiple users using a csv file: https://lazyadmin.nl/powershell/set-aduser/

If you started with your script and need some more help please ask and post your script (using the CodeSample option of the Q&A editor for the script (the icon with 101010)) together with the error message or details what isn't working properly.


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

Regards
Andreas Baumgarten

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.