Bulk Import Active Directory User Properties with Windows PowerShell

Recently I had the impulse to populate existing AD user object property fields with phone numbers, titles, companies, and office; however, I wanted to use PowerShell to bulk import instead of the old ldifde method or manual labor. It was actually quite easy, but it does require some time massaging the CSV file in Excel.

  • First, export a CSV containing the users and fields you want to update (it will be our template). Our anchor is going to be the SID because it is guaranteed to be unique across the forest.

 

Get-ADGroupMember "UserGroup" | get-aduser -Properties samAccountName, name, sid, telephoneNumber, mobile, title, Company, physicalDeliveryOfficeName | Select-Object samAccountName, name, sid, telephoneNumber, mobile, title, Company, physicalDeliveryOfficeName | export-csv users-to-update.csv -NoTypeInformation

 

  • Open that file up in Excel and start populating your blank columns. Feel free to sort it however you like.

  • Now the fun part. It’s time to import our populated CSV file back into Active Directory. The Set-ADUser cmdlet will use the Replace switch so that data will be replaced if it already exists. Remember, the Identity ($_.sid) is our anchor which will match the CSV data to the AD user object.
Import-Csv -Path "users-to-update.csv" | ForEach-Object {Set-ADUser -Identity $($_.sid) -Replace @{telephoneNumber=$($_.telephoneNumber); mobile=$($_.mobile); title=$($_.title); Company=$($_.Company); physicalDeliveryOfficeName=$($_.physicalDeliveryOfficeName)}}

Since you’ve saved so much time updating your user objects, you can go back to playing Galaga (^_^))))))))…

Share
Disqus Comments Loading...

Recent Posts

Bittorrent IP Blocklists

In addition to using a VPN service, as an extra precaution I've been using the blocklist feature of my bittorrent… Read More

October 26, 2019 3:31 pm

FreeNAS Error Creating Pool

command '('gpart', 'create', '-s', 'gpt', '/dev/da8')' returned non-zero exit status 1. If you get this error while trying to create… Read More

June 7, 2019 3:44 pm

Change Grub Default Boot Entry on Linux Mint

I'm dual booting Windows and Linux Mint on my laptop. The grub default is to boot into Linux Mint, however… Read More

April 23, 2019 7:45 pm

How to Reset Secure Channel On Active Directory Domain Controller

When you're a little too careless about virtualizing your domain controllers, cloning, migrating, backing up and restoring, returning from vacation… Read More

April 21, 2019 8:14 am

Run Systemd Script Before System Shutdown

I tried to retain the NGINX FastCGI cache and have it persist across system reboots instead of being ephemeral by… Read More

April 20, 2019 10:14 am

Learn Systemctl Usage to Manage Systemd Service in Linux

Systemd is new service manager for Linux. It's a replacement for all previous init systems (SysV/SysVinit & Ubuntu's Upstart) and… Read More

April 20, 2019 7:55 am