Updating and appending a CSV

%3CLINGO-SUB%20id%3D%22lingo-sub-3213207%22%20slang%3D%22en-US%22%3EUpdating%20and%20appending%20a%20CSV%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3213207%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EI%20have%20a%20task%20I'd%20like%20to%20complete%20using%20Powershell.%20I'm%20not%20a%20complete%20noob%2C%20but%20not%20intermediate%2C%20let's%20say%20post%20beginner%20when%20it%20comes%20to%20.csv%20data%20manipulation.%3C%2FP%3E%3CP%3EI%20know%20what%20I'd%20like%20to%20achieve%2C%20but%20I'm%20not%20entirely%20sure%20how%20best%20to%20achieve%20it.%20I%20also%20have%20a%20tendency%20to%20over%20think%20my%20scripting.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhile%20I've%20no%20doubt%20someone%20can%20quickly%20give%20me%20a%20script%20to%20solve%20this%20task%20I'd%20like%20to%20learn%20how%20I%20should%20approach%20solving%20this%20type%20of%20task%20myself.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20task...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20a%20.csv%20with%205%20columns%2C%20each%20with%20headers%2C%20and%20a%20few%20thousand%20rows.%20The%20first%20column%20has%20usernames%2C%20each%20ending%20with%20'%24'.%20I%20would%20like%20to%20find%20specific%20details%20for%20each%20username%20(taken%20from%20AD)%20and%20add%2Fappend%20this%20data%20to%20the%20.csv.%20The%20usernames%20will%20need%20the%20'%24'%20removed%20for%20searching.%3C%2FP%3E%3CP%3EI've%20tried%20to%20break%20this%20task%20down%20into%20steps%20to%20see%20if%20it%20will%20keep%20me%20from%20feeling%20overwhelmed.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESteps%3CBR%20%2F%3E1.%20extract%20usernames%20listed%20in%20column%201%20and%20removed%20'%24'%20from%20the%20end%20of%20each%20username.%3CBR%20%2F%3E2.%20check%20for%20username%20in%20AD%3CBR%20%2F%3Eif%20found%20select%20displayname%2C%20givenname%2C%20surname.%3CBR%20%2F%3E3.%20create%20new%20columns%2C%20with%20headers%2C%20for%20each%20object%20(displayname%2C%20givenname%2C%20surname)%20in%20the%20existing%20.csv%2C%20unless%20the%20column%20already%20exists.%3CBR%20%2F%3Einsert%20information%20selected%20at%20step%202.%3CBR%20%2F%3EIf%20no%20information%20was%20found%20at%20step%202%20insert%20%22no%20information%20found%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20unsure%20of%20the%20correct%20approach%20to%20take%20and%20as%20mentioned%20previously%20I%20tend%20to%20over%20think%20my%20scripts.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20puzzles%20me%20the%20most%20is%20how%20do%20I%20add%2Fappend%20the%20new%20data%20to%20the%20.csv%20at%20the%20correct%20row%2Fcolumn%20while%20also%20only%20creating%20the%20column%20headers%20once%20and%20making%20sure%20to%20insert%20%22no%20information%20found%22%20if%20nothing%20is%20found%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20guidance%20most%20welcome.%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3213207%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

Hello,

I have a task I'd like to complete using Powershell. I'm not a complete noob, but not intermediate, let's say post beginner when it comes to .csv data manipulation.

I know what I'd like to achieve, but I'm not entirely sure how best to achieve it. I also have a tendency to over think my scripting.

 

While I've no doubt someone can quickly give me a script to solve this task I'd like to learn how I should approach solving this type of task myself.

 

The task...

 

I have a .csv with 5 columns, each with headers, and a few thousand rows. The first column has usernames, each ending with '$'. I would like to find specific details for each username (taken from AD) and add/append this data to the .csv. The usernames will need the '$' removed for searching.

I've tried to break this task down into steps to see if it will keep me from feeling overwhelmed.

 

Steps
1. extract usernames listed in column 1 and removed '$' from the end of each username.
2. check for username in AD
if found select displayname, givenname, surname.
3. create new columns, with headers, for each object (displayname, givenname, surname) in the existing .csv, unless the column already exists.
insert information selected at step 2.
If no information was found at step 2 insert "no information found".

 

I am unsure of the correct approach to take and as mentioned previously I tend to over think my scripts.

 

What puzzles me the most is how do I add/append the new data to the .csv at the correct row/column while also only creating the column headers once and making sure to insert "no information found" if nothing is found?

 

Any guidance most welcome.

Thanks.

1 Reply

Hello @user4444,

Assuming that input file looks to something similar to this:

 

Username         Colum2 Column3 Column4 Column5
--------         ------ ------- ------- -------
tony.stark$      Data   Data    Data    Data
steve.rogers$    Data   Data    Data    Data
bruce.banner$    Data   Data    Data    Data
…$               …      …       …       …
natasha.romanov$ Data   Data    Data    Data
wanda.vision$    Data   Data    Data    Data
peter.parker$    Data   Data    Data    Data

 

One of the approaches would be the following:

 

$InputFile=Import-csv .\Users.csv
foreach($i in $InputFile){
 $UserInfo=Get-ADUser $i.Username.replace('$','')
 if($UserInfo){
   $i | Add-Member -NotePropertyName DisplayName -NotePropertyValue $($i.Displayname)
   $i | Add-Member -NotePropertyName GivenName -NotePropertyValue $($i.GivenName)
   $i | Add-Member -NotePropertyName Surname -NotePropertyValue $($i.Surname)
   $i | Add-Member -NotePropertyName Notes -NotePropertyValue ""
}else{
   $i | Add-Member -NotePropertyName DisplayName -NotePropertyValue ""
   $i | Add-Member -NotePropertyName GivenName -NotePropertyValue ""
   $i | Add-Member -NotePropertyName Surname -NotePropertyValue ""
   $i | Add-Member -NotePropertyName Notes -NotePropertyValue "No Information Found"  
 }
 
}

 

 

After that you can Export-Csv and have a file with consolidated Data.

Hope that helps.