# Export ADUser attribute

**URL:** <https://forums.powershell.org/t/export-aduser-attribute/7412>\
**Category:** PowerShell Help\
**Created:** [October 26, 2016, 1:28pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412 "2016-10-26T13:28:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [October 26, 2016, 1:28pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/1 "2016-10-26T13:28:10Z")

</div>

I tried this code to dump an attribute from a collection .csv and only getting the last User

```
Import-CSV "C:\updatedSamAccountName.csv" | % { 
$User = $_.samAccountName
Get-ADUser $User -Credential $creds -Properties EmployeeID |
Export-CSV "C:\BackupEmployeeID.csv" -NoTypeInformation
}
```

What am I doing incorrectly?

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [October 26, 2016, 1:30pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/2 "2016-10-26T13:30:35Z")

</div>

then I want to update an attribute for all of these users

```
Import-CSV "C:\updatedSamAccountName.csv " | % { 
$User = $_.samAccountName
$ID = $_.EmployeeID
Set-ADUser $User -employeeID $ID 
}
```

Will this update ALL the users in the .csv? I would think the forEach would ensure this.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [October 26, 2016, 1:34pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/3 "2016-10-26T13:34:01Z")

</div>

the export-csv goes outside the loop.

second question yes. you don’t need to set variables and you don’t need the qoutes around the csv.

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [October 26, 2016, 1:36pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/4 "2016-10-26T13:36:15Z")

</div>

Import-CSV C:\updatedSamAccountName.csv | % { Get-ADUser $\_.samAccountName -Properties EmployeeID} | Export-CSV C:\BackupEmployeeID.csv -NoTypeInformation

---

<div class="post-metadata">

**Author:** ![jeff-taylor](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/jeff-taylor/32/1015_2.png) [@jeff-taylor](https://forums.powershell.org/u/jeff-taylor)\
**Post date:** [October 26, 2016, 4:39pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/5 "2016-10-26T16:39:23Z")

</div>

Thanks Dan, the export work.

re: Question #2 Not seeing how I will will be able to populate the employeeID with the imported csv if I _don’t_ use the set variables for same.

If I reduce it to your suggestion:

```
Import-CSV "C:\updatedSamAccountName.csv " | % { 
Set-ADUser -employeeID
}
```

This will update the employeeID for all imported objects?

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [October 26, 2016, 6:44pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/6 "2016-10-26T18:44:16Z")

</div>

$\_ always confuses beginners 😀

$\_ is the object…the whole line in your csv. $\_.columnheader is the specific property of that object.

So set-aduser -employeeid $\_.employeeid

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [October 26, 2016, 8:25pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/7 "2016-10-26T20:25:43Z")

</div>

The issue with your Set-ADUser is it doesn’t know who to set, so you would do something like this:

```
foreach ($user in (Import-CSV "C:\updatedSamAccountName.csv")) {
    #You have to tell Set-ADUser who to set with Identity. You can
    #also pipe Get-ADUser results to Set-ADUser (see below)
    Set-ADUser -Identity $user.SamAccountName -employeeID $user.EmployeeID
}
```

However, if you are doing something quick and don’t care if something fails you shouldn’t use aliases and one-liners. Take a look at this example and follow the logic:

```
foreach ($user in (Import-CSV "C:\updatedSamAccountName.csv")) {
    #Search for the SamAccountName in AD
    $user = Get-ADUser -Filter ("SamAccountName -eq {0}" -f $user.SamAccountName)
    #If SamAccountName is not NULL
    if ($user) {
        try {
            #We don't need to specify the Identity because we are piping the found
            #user to Set-ADUser. In order to trap the error, we need to set ErrorAction
            #to Stop
            $user | Set-ADUser -EmployeeID $user.EmployeeID -ErrorAction Stop
        }
        catch {
            #An error occured and we capture it
            "There was an issue setting employee ID {0} for user {1}. {2}" -f $user.EmployeeID, $user.SamAccountName, $_
        }
    }
    else {
        #$user is NULL, so the user wasn't found
        "{0} user not found" -f $user.SamAccountName
    }
    
}
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [October 27, 2016, 6:51am UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/8 "2016-10-27T06:51:14Z")

</div>

$user got lost between the first question and the second=D.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/export-aduser-attribute/7412/9 "2024-05-16T20:40:09Z")

</div>


