From da6c6948dda8c665822adb4c39dc3d64983dd1fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Nov=C3=A1k?= Date: Wed, 20 Mar 2024 21:02:36 +0000 Subject: [PATCH] Add replace-attributes --- replace-attributes | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 replace-attributes diff --git a/replace-attributes b/replace-attributes new file mode 100644 index 0000000..e77fbbd --- /dev/null +++ b/replace-attributes @@ -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: $_" + } +} \ No newline at end of file