Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell - Add content from one CSV-file to another IF user exist in both files

$
0
0

Greetings,

I have two CSV-files, file.csv and ad.csv.

file.csv"DisplayName","PrimarySmtpAddress","WhenMailboxCreated","IssueWarningQuota","Total Size (MB)","KAP""gemensam","gemensam@xyxy.se","2012-12-18 19:38:31","3.809 GB (4,089,446,400 bytes)","4""Göran Göransson","göran@xyxy.se","2012-12-18 18:52:32","3.809 GB (4,089,446,400 bytes)","974""Lars Snabelsson","lars@xyxy.se","2012-12-18 18:52:07","7.813 GB (8,388,608,000 bytes)","4451"


ad.csv

"name","whencreated""Göran Göransson","2008-12-18 16:09:38""Lars Snabelsson","2013-04-15 07:13:14"


What I want to do with powershell is to check if the users in ad.csv exists in file.csv. If a user exist in both files the column "KAP" (file.csv) shall get the value "X" for the specific user, and the "whencreated" column shall be added and the value for the specific user. I don't know if it is possible to ad this to file.csv, or if it's better to create a new csv-file?

So far I've manage to come this far:

$userData2 = Import-Csv E:\Simon\file.csv $appendData2 = Import-Csv E:\Simon\ad.csv $userNameNr = -1 $userNames = $userData2.DisplayName $appendNames = $appendData2.name $appendNameNr = $appendNames.Count $userNames | Foreach { $appendNameNr = $appendNames.Count $userNameNr += 1 $userName = $userNames[$userNameNr] $appendNames | Foreach { $appendNameNr += -1 $appendName = $appendNames[$appendNameNr] if ($userName -like $appendName) { Write-Host "Score! $appendName exist in both files." #Here I would like to add the value "X" to the user $appendName for the column "KAP".

# And also add "whencreated" to the specific user. } else { #echo "Miss" } } }

The code works, it echo:s the users which are present in both ad.csv and file.csv. Now I need to add the "KAP"-value for the user, and "whencreated"-value. But I have no idea how...

Any ideas?

Thanks in advance!




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>