Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

How to set a value in extensionAttribute AD attribute by PowerShell?

Hi,

I have a list of samaccount with their extensionAttribute values in a csv file.
I want to create a Powershell script which checks AD attribute "extensionAttribute2"  of each user and add his corresponding extensionAttribute values.

My problem is with extensionAttribute  we need to play with ADD, Remove, Clear, Replace parameters and that mixe me up.

If just want to add if there is no value and if there is a value then replace it. But why there is  remove and clear parameter?

Is someone can help me for that?

Thanks
Avatar of oBdA
oBdA

In this case, just use -Replace:
Import-Csv -Path "C:\Temp\users.csv" | ForEach-Object {
	Set-ADUser -Identity $_.SamAccountName -Replace @{'extensionAttribute2' = $_.extensionAttribute}
}

Open in new window

Remember that there are attributes that hold arrays. -Remove will remove (only) the values listed in the argument, and Clear will clear all contents of the attribute.
Avatar of SAM2009

ASKER

But why don't we use also "ADD" parameter? If there is no value we need to use ADD first no?
No. It's the same as with -Remove: you need to use -Add if you want to add more values to an already existing array of values for an attribute.
The attribute itself is always there.
SOLUTION
Avatar of Kevin Stanush
Kevin Stanush
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

Which comparison operator can I use to verify if the extensionAttribute is empty or not?

if (user.extensionAttribute -ne " " )

or

if (!user.extensionAttribute)
The first one will compare against an empty string.If the attribute is cleared, it will be $Null.
So use either
if ($user.extensionAttribute) {
	# Not empty
} Else {
	# Empty
}

Open in new window

Or this:
if ([string]::IsNullOrEmpty($user.extensionAttribute)) {
	# Empty
} Else {
	# Not empty
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

The reason why I asked that is because I want to update extensionAttribute in user AD from an csv file at every 30 min. So just I'm just wondering if it worth to verify if the value is different first before replacing it or just replace even if the value is the same. Maybe by skiping user that having their extensionAttribute not changed can safe time. What do you think?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

Thanks for your help!