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
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,898 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 45,091 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}
        }