Powershell - import CVS file to fill some attributes in AD

Riccardo Chiappini 41 Reputation points
2022-02-02T14:13:52.397+00:00

Hello,

i would like to import a .csv file to fill attributes in AD ( for example "ExtensioneAttribute2"); the import match the attribute "mail" for

I try with this two scripts, but without success:

1) Import-Csv c:\user\admin\desktop\test.csv | ForEach-Object {Get-ADUser -filter {mail -eq $.mail}|Set-ADUSer -extensionAttribute2 $.extensionAttribute2}

  • ERROR:

Set-ADUser : A parameter cannot be found that matches parameter name 'extensionAttribute2'.
At line:1 char:118

  • ... ser -filter {mail -eq $.mail}|Set-ADUSer -extensionAttribute2 $.exten ...
  • ~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Set-ADUser : A parameter cannot be found that matches parameter name 'extensionAttribute2'.
At line:1 char:118

  • ... ser -filter {mail -eq $.mail}|Set-ADUSer -extensionAttribute2 $.exten ...
  • ~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

2) Import-Csv c:\users\chiappini_dlv\desktop\test.csv | ForEach-Object {Set-ADUser $.mail ` -ObjectAttributes @{ExtensionAttribute2=($.ExtensionAttribute2)}}

  • ERROR:

Set-ADUser : A parameter cannot be found that matches parameter name 'ObjectAttributes'.
At line:1 char:91

  • ... .csv | ForEach-Object {Set-ADUser $_.mail ` -ObjectAttributes @{Exten ...
  • ~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Set-ADUser : A parameter cannot be found that matches parameter name 'ObjectAttributes'.
At line:1 char:91

  • ... .csv | ForEach-Object {Set-ADUser $_.mail ` -ObjectAttributes @{Exten ...
  • ~~~~~~~~~~~~~~~~~
  • CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
  • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Windows for business | Windows Client for IT Pros | Directory services | Active Directory
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-02-02T20:18:57.63+00:00

    "extensionAttribute2" is an attribute that's added to your AD schema by the installation of MS Exchange. Are you running an on-premises Exchange organization (or have you extended the AD schema using the Exchange installation media)? Or are you using Microsoft 365?

    Assuming you have the Exchange cmdlets available to you, use the Set-MailUser, Set-MailBox, Set-DistributionList, etc. cmdlets (as appropriate) and use the -CustomAttribute2 parameter.

    Where did you get the information that states that "-extensionAttribute2" or "-ObjectAttributes" are acceptable parameters for the Set-ADUser cmdlet?

    I believe this is what you were trying to accomplish:

    Import-Csv c:\user\admin\desktop\test.csv | 
        ForEach-Object {
            Get-ADUser -filter { mail -eq $._mail } |
                    Set-ADUser -Add @{extensionAttribute2=$_.extensionAttribute2}
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.