Lobi
Lobi

Reputation: 175

I created a new custom attribute in ActiveDirectory. How can I modify it in PowerShell?

I created a new custom attribute like: newattribute1, but when I want to change the value in PowerShell, I got an error.

Set-ADUser -Identity test1 -newattribute1 123as

The error message:

Set-ADUser : A parameter cannot be found that matches parameter name
'newattribute1'.
At line:1 char:29
+ Set-ADUser -Identity test1 -newattribute1 123as
+                            ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Upvotes: 1

Views: 4494

Answers (2)

Nick
Nick

Reputation: 1208

I always use:

Set-ADUser -identity <username> -replace @{CustomAttribute="YourData"}

By using the replace function, you can specify the custom attribute that you created. It an easy way to change attributes which cannot be specified by the cmdlet itself. This doesn't only work for custom attributes, you can use the replace function for attributes such as phone number. Anything that the cmdlet doesn't let you modify by default.

On a bit of a side note, you can't just make up parameters to add to an existing cmdlet like you had -newattribute1 123as.

Upvotes: 2

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10799

You will need to modify a copy of the ADUser object, then write the copy back using the -Instance parameter of Set-ADUser:

$user = Get-ADUser -Identity $samaccountname -Properties *
$user.YourCustomAttribute = $NewCustomAttributeValue
Set-ADUser -Instance $User

See Get-Help Set-ADUser.

Upvotes: 1

Related Questions