You do not need to hardcode any values. The clean approach is to query all users where extensionAttribute1 is populated, then run your logic based on the value.
First load the Active Directory module:
Import-Module ActiveDirectory
Then retrieve only users that have extensionAttribute1 set. This automatically excludes empty values:
$Users = Get-ADUser -Filter { extensionAttribute1 -like "*" } -Properties extensionAttribute1
Now process each user according to the attribute value:
foreach ($User in $Users) {
if ($User.extensionAttribute1 -eq "CW") {
Write-Output "Processing CW user $($User.SamAccountName)"
# CW logic here
}
elseif ($User.extensionAttribute1 -eq "Regular") {
Write-Output "Processing Regular user $($User.SamAccountName)"
# Regular logic here
}
elseif ($User.extensionAttribute1 -eq "SP") {
Write-Output "Processing SP user $($User.SamAccountName)"
# SP logic here
}
If you need to update something based on the value, place Set-ADUser inside the relevant block. Example:
Set-ADUser -Identity $User -Replace @{department = "Operations"}
For daily scheduling:
- Save the script as C:\Scripts\ProcessExtensionAttribute.ps1
- Create a Scheduled Task
- Run whether user is logged on or not
- Use highest privileges
- Program: powershell.exe
- Arguments: -ExecutionPolicy Bypass -File "C:\Scripts\ProcessExtensionAttribute.ps1"
Make sure the account running the task has permission to read and modify user objects.
This design:
Reads extensionAttribute1 dynamically Excludes users with empty values Applies logic based on the actual attribute value Is safe to run daily.
If this resolution has been helpful, I kindly request that you take a moment to click on
and select “Yes” to indicate that the response was helpful. Should you have any further questions or require additional assistance, please do not hesitate to let me know.