MMbill
MMbill

Reputation: 67

Pull SamAccountName into csv from list of employeeID's

$user = ForEach ($user in $(get-content C:\Users\desktop\employeeids.csv)) {

    Get-ADUser $user -properties employeeid
}

$users |

Select-object Samaccountname |

export-csv -path C:\users\desktop\usernames.csv

I am trying to pull the Samaccountname of a list of employee id's I was given and export those samaccountnames into a csv. This is what I have so far but I keep getting the following error

Get-ADUser : Cannot find an object with identity: '12345' under: 'DC=,DC='.
At line:3 char:5
+     Get-ADUser $user -properties employeeid
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (12345:ADUser) [Get-ADUser], ADIdentity 
   NotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Managem 
   ent.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.Get  
  ADUser

Upvotes: 0

Views: 1182

Answers (1)

Itchydon
Itchydon

Reputation: 2596

$users = ForEach ($user in $(get-content C:\Users\desktop\employeeids.txt)) {
Get-ADUser $user -properties employeeid
}
$users |
Select-object Samaccountname |
export-csv -path C:\users\desktop\usernames.csv

I have changed the original variable to $users instead of $user.
I have also changed the file extension to a .txt but if it is working for you using a .csv extension should be ok

Upvotes: 2

Related Questions