Add replace-attributes

This commit is contained in:
Ondřej Novák
2024-03-20 21:02:36 +00:00
commit da6c6948dd

23
replace-attributes Normal file
View File

@@ -0,0 +1,23 @@
# Import the Active Directory module
Import-Module ActiveDirectory
# Define source and destination attribute names as variables
$sourceAttribute = "personalTitle"
$destinationAttribute = "msDS-CloudExtensionAttribute1"
# Retrieve all users where the source attribute is not empty
$usersWithSourceAttribute = Get-ADUser -Filter "$sourceAttribute -like '*'" -Properties $sourceAttribute, $destinationAttribute
# Loop through each user and update the destination attribute with the source attribute's value
foreach ($user in $usersWithSourceAttribute) {
# Extract the value of the source attribute for readability
$sourceValue = $user.$sourceAttribute
try {
# Copy value from source attribute to destination attribute
Set-ADUser -Identity $user.DistinguishedName -Replace @{$destinationAttribute = $sourceValue}
Write-Host "Successfully updated user: $($user.SamAccountName)"
} catch {
Write-Host "Failed to update user: $($user.SamAccountName). Error: $_"
}
}