set mailNickName from csv

I’m trying to set a ‘not set’ value of the mailNickName attribute with this code:

$users = Import-Csv -Path .\ConfUsers.csv 
#has two columns samAccountName,mailNickNameValue
                      
foreach ($user in $users) {                        
 Get-ADUser -Identity $($user.samAccountName) -Properties mailnickname -Server domain.com |
    Set-ADUser -Identity $_ -Replace @{mailNickname=$_.mailNickNameValue}       
}

Error is on the Identity parameter.

For some reasons, dotted notation fails to work with AD cmdlet. replace this line.

$sam = $user.samAccountName; Get-ADUser -Identity $sam -Properties mailnickname -Server domain.com |
Set-ADUser : Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.

Also tried this:

$users = Import-Csv -Path .\ConfUsers.csv
foreach ($user in $users) {
    $sam = $user.samAccountName; Get-ADUser -Identity $sam -Properties * -Server domain.com | 
        Set-ADUser $user -Server domain.com -Replace @{ mailNickName = "$($user.mailNicknameValue)" }
}

…but get different error:

et-ADUser : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline 
input.

This should work:

Foreach ($user in $users)
    { 
      Get-ADUser -Identity $user.SamAccountName -Properties mailnickname `|
      % {Set-ADUser -Identity $PSItem -Replace @{mailnickname=$user.mailnicknamevalue}}
    }
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument,

Set-aduser accepts pipline input by value. Is there a specific reason for the initial “get-aduser”? You already have the samaccount and mailnickname values. Seems like you can accomplish the same by a simple:

$users = Import-Csv -Path .\ConfUsers.csv
foreach ($user in $users) {
    Set-aduser -Identity $user.samaccountname -Replace @{mailNickName = $user.mailNicknameValue}
}

Try this. This should resolve.

$users = Import-Csv -Path .\ConfUsers.csv
foreach ($user in $users) {
    $sam = $user.samAccountName
    $nickname = $user.mailNicknameValue 
    Get-ADUser -Identity $sam -Properties * -Server domain.com | Set-ADUser -Replace @{mailNickName = $nickname}
}

ah ok, thank you I see now, that did work. So I really didn’t need PSItem per your earlier script?

Nope - i noticed that after the fact.

Juan, where do I see that Set-ADUser accepts pipeline by value? I only see “false” for all parameters (there’s alot)

get-help Set-ADUser -Full

Its under the identity parameter:

 -Identity 
     Specifies an Active Directory user object by providing one of the following property values. The identifier in parentheses is the LDAP display name for the attribute.

     Distinguished Name

     Example:  CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com

     GUID (objectGUID)

     Example: 599c3d2e-f72d-4d20-8a88-030d99495f20

     Security Identifier (objectSid)

     Example: S-1-5-21-3165297888-301567370-576410423-1103

     SAM account name  (sAMAccountName)

     Example: saradavis

     The cmdlet searches the default naming context or partition to find the object. If two or more objects are found, the cmdlet returns a non-terminating error.

     This parameter can also get this object through the pipeline or you can set this parameter to an object instance.

     This example shows how to set the parameter to a distinguished name.

     -Identity  "CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com"

     This example shows how to set this parameter to a user object instance named "userInstance".

     -Identity   $userInstance

     Required?                    true
     Position?                    1
     Default value
     Accept pipeline input?       True (ByValue)
     Accept wildcard characters?  false

thanks again.