如何批量移除AD用户账户Description字段的指定内容?
Batch Remove Specific Text from AD User Descriptions via CSV
Here's a reliable solution to update your ~1000 AD users' Description fields—removing the exact phrase "Call security before re-enabling this account" while preserving all other content like job titles. This PowerShell script reads from your CSV and handles the bulk update smoothly:
Import-Module ActiveDirectory # Define the exact text you want to remove and a temporary replacement $targetPhrase = 'Call security before re-enabling this account.' $tempReplacement = ' ' # Import your CSV and process each user Import-Csv -Path "C:\users\your-path-here.csv" | ForEach-Object { # Retrieve the user object with the Description property included $adUser = Get-ADUser $_.'User-Name' -Properties Description # Update the Description: remove the target phrase and clean up extra whitespace Set-ADUser $adUser -Description $adUser.Description.Replace($targetPhrase, $tempReplacement).Trim() }
Key Details Explained:
- Active Directory Module: The first line loads the required module for AD cmdlets—make sure this is installed on your machine (it's part of RSAT tools).
- Variable Setup:
$targetPhraseis the exact text you want to eliminate. We use a space as a temporary replacement, thenTrim()to wipe out any leftover extra spaces that might result from the replacement. - CSV Processing:
Import-Csvreads your user list. Ensure your CSV has a column namedUser-Namematching the AD usernames—adjust the column name in the script if yours is different (likeSamAccountName). - User Retrieval:
Get-ADUserfetches each user object, explicitly requesting theDescriptionproperty (since it's not part of the default set of properties returned). - Description Update: The
Replace()method removes the unwanted text, andTrim()ensures the final Description is clean without trailing/leading spaces.
Pro Tips:
- Test First: Run this on a small subset of users (e.g., add a
Where-Objectclause to filter one user) before applying to all 1000 to verify it works as expected. - Permissions: Ensure your account has the necessary rights to modify AD user attributes (typically "Write Description" permission on the OU containing the users).
内容的提问来源于stack exchange,提问作者griffen62
相关产品推荐
相关产品推荐

