Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

append column with AD info to CSV

$
0
0

Hello,

I apologize in advance if this is mind-numbingly boring for you folks... I'm just starting to learn Powershell and have hit a roadblock.  What I'm trying to accomplish is this:

Read from a CSV (that has two columns) and perform a search in AD based on the data in the second column.  I then want to to append the additional information to the existing csv columns.

The CSV file looks like this:

Machine_Name, CN

comp111,jdoe

comp222,jsmith

 

The script I have limping along is:

$Users = @()

Import-Csv C:\tempfolder\powershell\test.csv | % {


#define filter variable from second column in CSV

$f = $_.CN;
$Users += get-aduser -filter { CN -eq $f } -Properties CN,displayname,co,City,StreetAddress,LastLogonDate | select-object CN,displayname,co,City,StreetAddress,LastLogonDate


}
$Users | Select Computer_Name,CN,displayname,co,City,StreetAddress,LastLogonDate | Export-Csv C:\tempfolder\powershell\newtest.csv -NoTypeInformation

The output I get has all of the correct headers.. but the "Machine_Name" column is empty (the other columns populate correctly). I humbly begged, borrowed, and stole this code from forums and edited it for the information I needed.

Ideally I would like to get an output like this:

Machine_Name,CN,displayname,co,City,StreetAddress,LastLogonDate

comp111,jdoe,"john doe", USA,NY,"123 awesome street","6/30/2015"

 

Any help would be greatly appreciated!  I do not ask for your help in complete ignorance, I just ordered a book on learning powershell!  Thanks for your time & help.


Viewing all articles
Browse latest Browse all 6937

Trending Articles