I'm looking for a way to read csv file for header 'Customer' and 'Cars ID'. Issue is I have duplicate customer. I need to find a way to list each unique customer and all its cars id with it. If possible to export it as CustomerName and list all cars id under it. So if I have 3 unique customer, each customer will be export separately.
#Get unique customer
$GetUniqCustomer = Import-Csv $File |
Sort-Object {$_.customer} -Unique |
Select {$_.customer}
From here on I'm not sure how I would do what I've described.
This will list all carid under the specific.
Import-Csv $File | Where-Object {
$_.customer -eq $Customer
} | Select {$_."carid"}
For your particular scenario you want to use Group-Object rather then Sort-Object -Unique. Build new custom object from the grouped information and export them to the output CSV file.
Import-Csv $File | Group-Object customer | ForEach-Object {
New-Object -Type PSObject -Property #{
Customer = $_.Name
Cars = ($_.Group | Select-Object -Expand carid) -join ';'
}
} | Export-Csv 'C:\path\to\output.csv' -NoType
Related
I have a csv file where I am trying to export rows into another csv file only where the values in the id column have duplicates.
I have the following csv file...
"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valThree","valFour"
"valFive","valSix"
"valFive","qwreweq"
"valSeven","valEight"
I need the output csv file to look like the following...
"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"
Here is the code I have so far:
$inputCsv = Import-CSV './test.csv' -delimiter ","
#$output = #()
$inputCsv | Group-Object -prop id, blablah | Where-Object {$_.id -gt 1} |
Select-Object
##{n='id';e={$_.Group[0].id}},
##{n='blablah';e={$_.Group[0].blablah}}
#Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation
#Write-Host $output
#$output | Export-Csv 'C:\scripts\powershell\output.csv' -NoTypeInformation
I've searched multiple how-to's but can't seem to find the write syntax. Can anyone help with this?
Just group on the ID property and if there is more than 1 count in the group then expand those and export.
$inputCsv = Import-CSV './test.csv' -delimiter ","
$inputCsv |
Group-Object -Property ID |
Where-Object count -gt 1 |
Select-Object -ExpandProperty group |
Export-Csv output.csv -NoTypeInformation
output.csv will contain
"id","blablah"
"valOne","valTwo"
"valOne","asdfdsa"
"valFive","valSix"
"valFive","qwreweq"
I have a CSV file which contains, let's say 50 different columns with 1000 rows. I don't need all of this information though and would like to now parse through it and remove the columns for which I do not want.
I want to keep every row (users) so I shouldn't be removing those wholesale, however, I have about a dozen or so columns for which I need to remove the data. How can I do this?
Ex.
User1 | Name | Age | Location | Gender | HairColor
keep | keep | remove | remove | keep | remove
If you want the columns removed completely:
$CSV = Import-Csv $Path | Select-Object -Property User1, Name, Gender
$CSV | Export-Csv $NewPath -NoTypeInformation
Or this, if it's easier:
$CSV = Import-Csv $Path | Select-Object -Property * -ExcludeProperty Age, Location, HairColor
$CSV | Export-Csv $NewPath -NoTypeInformation
If you want the columns to remain but be empty:
$CSV = Import-Csv $Path | Select-Object -Property User1, Name, #{n='Age';e={}}, #{n='Location';e={}}, Gender, #{n='HairColor';e={}}
$CSV | Export-Csv $NewPath -NoTypeInformation
I am trying to compare 2 csv files using 'user Id' column which is common in both the files and want to append the missing entries in csv 1 from csv 2 using power shell
If the first CSV file has missing rows, and they need to be copied in from the second CSV file directly:
ipcsv 2.csv |? 'User Id' -notin (ipcsv 1.csv).'User Id' | epcsv 1.csv -Append
If you're also saying that User IDs are unique in your files, you could instead take the unique rows out of the set of both CSVs into the first CSV:
(ipcsv 1.csv, 2.csv) | sort -Unique 'User Id' | epcsv 1.csv -NoTypeInformation
Alternatively, if you're saying that the files have the same User Ids but don't have the same other columns, and the entries in the first CSV are present but have some empty columns, then:
$csv1 = Import-Csv d:\file1.csv
$csv2 = Import-Csv d:\file2.csv
foreach ($csv1item in $csv1)
{
$csv2item = $csv2.Where{$_.'User Id' -eq $csv1item.'User Id'}
$item1Properties = $csv1item | Get-Member -MemberType NoteProperty | select -ExpandProperty Name
$item2Properties = $csv2item | Get-Member -MemberType NoteProperty | select -ExpandProperty Name
$sharedProperties = $item1Properties.where{$_ -in $item2Properties}
$sharedProperties | ForEach-Object {
$value = $csv1item."$_"
if ($value -eq '') {
$csv1item."$_" = $csv2item."$_"
}
}
}
$csv1 | Export-Csv D:\file1.csv -NoTypeInformation
Alternatively again, if you're saying the two files have different columns which overlap at 'User Id' and also the entries are missing from the first CSV entirely, rather than being present with missing columns, then this will add them from the second CSV, taking only the properties which overlap between them (hopefully):
$csv1 = Import-Csv d:\file1.csv
$csv2 = Import-Csv d:\file2.csv
$item1Properties = ($csv1[0] | gm -M NoteProperty).Name
$newCsv1Items = foreach($csv2item in $csv2.Where{$csv1.'User Id' -notcontains $_.'User ID'}) {
$newcsv1Item = #{}
$item1Properties.ForEach{$newcsv1Item."$_" = $csv2item."$_"}
[PSCustomObject]$newcsv1Item
}
$newCsv1Items | Export-Csv -Append D:\file1.csv
I have a .csv file which looks like
ID, Dept
1,x
1,y
1,z
2,a
2,b
2,c
output should be
ID, Dept
1, x;y;z
2, a;b;c
I tried the with below in the PowerShell but it is returning 0 for both columns
$csvValues = Get-Content "DeptDetails.csv"
$duplicates = $csvValues | group-object ID | ? Count -gt 1
$objs = New-Object System.Collections.ArrayList
ForEach ($duplicate in $duplicates){
$objs.Add([pscustomobject]#{ID = ($duplicate.Group.ID | select -Unique) -as [int];
GroupName=($duplicate.Group.Dept | ? Length -gt 0) -join ';'})
}
$objs | Sort ID
Use the Import-Csv cmdlet to load your csv. Group the entries by its ID using the Group-Object cmdlet. Then you can iterate over the group using the Foreach-Object cmdlet and create your desired Object for each ID. Finally export it back to csv using the Export-Csv cmdlet:
Import-Csv 'DeptDetails.csv' | Group-Object ID | ForEach-Object {
[PsCustomObject]#{
ID = $_.Name
Dept = $_.Group.Dept -join ';'
}
} | Export-Csv 'DeptDetails_output.csv' -NoTypeInformation
How do I read only the head from a CSV file and write the columnn names into an array?
I have found a solution using following cmdlets:
$obj = Import-Csv '.\users.csv' -Delimiter ';'
$headerarray = ($obj | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name')
But the problem is the name - values are auto sorted alphabetic
Anyone has a solution for this?
You can get the column names of a CSV file like this:
import-csv <csvfilename> |
select-object -first 1 | foreach-object { $_.PSObject.Properties } |
select-object -expandproperty Name