Powershell query to remove Bulk AD/LDAP users attribute to blank

Originally posted on May 7, 2022 @ 2:16 pm

Powershell query to clear up Active Directory user’s attribute in Bulk

In the last article, we saw how to export all users from Active Directory to an excel with the attributes you need. In case you missed to read the article, please follow this link.

In this article, we will create a CSV file for the users for whom we need to remove or clear the field for some specific attributes such as IPPhone, HomePhone etc.

My CSV will just have SAMAccountName as shown in the screenshot below:

uccollabing.com

 

I will name this csv as users1.csv and place this csv file in C:\ directory.

uccollabing.com

Here is the IP Phone and HomePhone attributes for the users.

uccollabing.com

As per the excel csv, we are going to apply the changes to Tom, John and Harry but not Chris. So when we run the bulk clearance attribute of Home and IP Phone number, it should not clear from Chris account but should clear from Tom, Harry and John’s account.

Powershell Script to clear the attributes in bulk:

uccollabing.com

$users = Import-Csv -Path C:\users1.csv
# Loop through CSV and update users if the exist in CVS file

foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “cn=Users,DC=uccollabing,DC=com” |
Set-ADUser -Clear l, ipphone, homephone
}

Time to see the results:

uccollabing.com

The results clearly shows that Chris account is intact with the IP Phone and Home Phone number whereas the users in the CSV file has been executed and the values were cleared successfully.

Hope you find it helpful!

1 thought on “Powershell query to remove Bulk AD/LDAP users attribute to blank”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top