0

I am currently working with a CSV file that has a manager's employee number, but not their SAMAccountName. I want to utilize Get-ADUser to grab the manager's SAMAccountName from their EmployeeNumber attribute and place that inside a new column in the same CSV file.

CSV sample:

"Full Name","Username","Manager","Manager User Sys ID"
"User 1","u1","1, Manager","123456"
"User 2","u2","2, Manager","234567"
"User 3","u3","3, Manager","345678"

I would like:

"Full Name","Username","Manager","Manager User Sys ID","Manager SamAccountName"
"User 1","u1","1, Manager","123456","m1"
"User 2","u2","2, Manager","234567","m2"
"User 3","u3","3, Manager","345678","m3"

I have spent some time putting together the following code. I can get a new column added and can further grab the SAMAccountName, but it only exports a single line in the new CSV file like this:

"SAMAccountName","managerUsername"
"m1","@{SAMAccountName=m1}"

Here is the code:

$managers = Import-Csv -Path .\test.csv
$usermananger = @()
foreach ($manager in $managers)
{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($manager."Manager User Sys ID")" -Properties SAMAccountName |
    Select-Object SAMAccountName
  $exportstring = $testmanager |
    Select-Object *,@{Name='managerUsername';Expression={"$getmanagersam"}}
  $exportstring | Export-Csv -Path .\simpletest.csv -NoTypeInformation
}
2
  • Change select SAMAccountName to select -ExpandProperty SAMAccountName Commented Apr 30, 2015 at 16:55
  • Mathias, thanks for the reply. That helped a bit - the output is cleaner but basically what's happening is it's throwing a syntax error and it's overwriting the CSV over and over and only keeps one line. Error parsing query 'employeeNumber -eq NO_MANAGER' syntax error. Commented Apr 30, 2015 at 17:38

3 Answers 3

1

As @MathiasR.Jessen mentioned in the comments: you need to expand the SamAccountName property to get just the value. Also, you're overwriting your output CSV with every iteration. Either append to the file, or move the Export-Csv cmdlet outside the loop. The former requires PowerShell v3 or newer, the latter requires that you change the loop to a ForEach-Object loop (or run the foreach loop in a subexpression).

Personally I'd prefer using a pipeline, so I'd pick the latter:

Import-Csv -Path .\test.csv | ForEach-Object {
  $acct = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" |
          select -Expand SamAccountName
  $_ | select *,@{Name='managerUsername';Expression={$acct}}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation
Sign up to request clarification or add additional context in comments.

2 Comments

Ansgar - this worked brilliantly. I appreciate the assist on this. One problem I'm having is that it keeps throwing a syntax error. Any advice on how to get around the syntax error?
@jdubs You have one or more blank lines in test.csv. Remove them.
0

The short answer is to add the -Append option to your export-csv statement to stop it overwriting each time round the loop.

Alternatively move the export outside the loop as follows:

$managers = Import-Csv -Path .\test.csv

$managers|foreach-object{
  $getmanagersam = Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')" | select -ExpandProperty SAMAccountName
  $_|Select *,@{Name='managerUsername';Expression=$getmanagersam}
} | Export-Csv -Path .\simpletest.csv -NoTypeInformation

Note: looks like @AnsgarWiechers beat me to it :-)

Comments

0

I'm late to the party it looks like, but that rarely stops me... I'm not a huge fan of adding properties via the Select cmdlet, so I'm going to offer an alternative where you pipe the CSV to a ForEach-Object loop (just like the other answers), but inside that you use Add-Member with the -PassThru argument, and then pipe it off to the output file. I will add a new line and indent after the pipes, just to make it easier to read.

Import-Csv -Path ".\test.csv" | 
    ForEach-Object {Add-Member -InputObject $_ -NotePropertyName 'managerUserName' -NotePropertyValue (Get-ADUser -Filter "employeeNumber -eq $($_.'Manager User Sys ID')").samaccountname -PassThru} |
    Export-Csv -Path ".\simpletest.csv" -NoTypeInformation

This should essentially do the exact same thing as the other two answers, it just adds the property differently, because variety is the spice of life! (I saw that on the internet somewhere, so it must be true.)

2 Comments

MadTech - This also worked brilliantly, thank you for adding to the discussion. This method is throwing the same syntax error as Angars version... do you think it has something to do with the CSV header having spacing?
it probably wants single quotes around the search string. Try "employeeNumber -eq '$($_.'Manager User Sys ID')'" and see if that doesn't fix the error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.