Export-CSV Filtering - powershell

$Headers = "Username"
Import-CSV -Path "$WorkingFolder\Test.csv" | Select $Headers | Export-CSV -Path "$WorkingFolder\Master.txt" -Force -NoTypeInformation
I am exporting a CSV File as a .txt file and only want to select the data from the Username column. It succesfully outputs as a .txt file and select all the items listed as Usernames. but contains "Information" and the header in the .txt file. I just want the raw data and want to remove the quotation marks and the header. I understand already that I can just skip the first row for exporting if need be but was going to see if there was another way round this.

I guess then what you want is
(Import-Csv -Path "$WorkingFolder\Test.csv").UserName |
Set-Content -Path "$WorkingFolder\Master.txt"
P.S. Personally, I would rather use Join-Path -Path $WorkingFolder -ChildPath 'test.csv' to make sure you get the path separator correct

Related

Powershell - Extract first line from CSV files and export the results

Thanks in advance for the help.
I have a folder with multiple CSV files. I’d like to be able to extract the first line of each of the files and store the results in a separate CSV file. The newly created CSV file will have the first column as the file name and the second column to be the first line of the file.
The output should look something like this (as an exported CSV File):
FileName,FirstLine
FileName1,Col1,Col2,Col3
FileName2,Col1,Col2,Col3
Notes:
There are other files that should be ignored. I’d like the code to loop through all CSV files which match the name pattern. I’m able to locate the files using the below code:
$targetDir ="C:\CSV_Testing\"
Get-ChildItem -Path $targetDir -Recurse -Filter "em*"
I’m also able to read the first line of one file with the below code:
Get-Content C: \CSV_Testing\testing.csv | Select -First 1
I guess I just need someone to help with looping through the files and exporting the results. Is anyone able to assist?
Thanks
You basically need a loop, to enumerate each file, for this you can use ForEach-Object, then to construct the output you need to instantiate new objects, for that [pscustomobject] is the easiest choice, then Export-Csv will convert those objects into CSV.
$targetDir = "C:\CSV_Testing"
Get-ChildItem -Path $targetDir -Recurse -Filter "em*.csv" | ForEach-Object {
[pscustomobject]#{
FileName = $_.Name
FirstLine = $_ | Get-Content -TotalCount 1
}
} | Export-Csv path\to\theResult.csv -NoTypeInformation
I have assumed the files actually have the .Csv extension hence changed your filter to -Filter "em*.csv", if that's not the case you could use the filter as you currently have it.

Powershell Compare 2 CSV but highlight duplicates or create a new column

I'm trying to compare 2 csv files and highlight the duplicates or add a separate column flag with text to let them know it is a duplicate. Right now I can generate a separate csv with only the changes but I would like to keep everything in the original csv and I just wanted to see if it could be done.
Here is my code to remove the duplicates into another csv:
$csv2 = (Import-Csv -Path \.csv).Primaryemail
$removedDuplicateRows = Import-Csv -Path \.csv | Where-Object {$_.Associate_Email -in $csv2}
$removedDuplicateRows | Export-Csv -Path \.csv -NoTypeInformation

Adding Extra Headers in CSV

Input CSV:
CHeader1,CHeader2,CHeader3,CHeader4,CHeader5
a1,a2,a3,a4,a5
b1,b2,b3,b4,b5
Output CSV:
PHeader1,PHeader2
CHeader1,CHeader2,CHeader5
a1,a2,a5
b1,b2,b5
Already tried
Import-Csv -Path .\before.csv |
select CHeader1, CHeader2, CHeader5 |
Export-Csv -Path .\after.csv
This produces the file without parent level headers.
Any suggestions to add parent level headers in first line of CSV followed by client headers and then the data?
What you're trying to create there is not actually a CSV, at least not in a way that the *-Csv cmdlets could handle. You can manually create it like this, though:
'PHeader1,PHeader2' | Set-Content '.\after.csv'
Import-Csv '.\before.csv' |
Select-Object CHeader1, CHeader2, CHeader5 |
ConvertTo-Csv -NoType |
Add-Content '.\after.csv'

Append filename to CSV in new column

I have a lot of csv files stored in a directory.
In each csv file need to add the name as column this through powershell.
Example
File location: <SERVERNAME>\Export\FILENAME1.CSV
Contents:
1232;Description;a1
1232;Description;a2
The result must be:
1232;Description;a1;FILENAME1.CSV
1232;Description;a2;FILENAME1.CSV
Can someone help me with this?
The following will append a Filename column to each .CSV file in a directory:
Get-ChildItem *.csv | ForEach-Object {
$CSV = Import-CSV -Path $_.FullName -Delimiter ";"
$FileName = $_.Name
$CSV | Select-Object *,#{N='Filename';E={$FileName}} | Export-CSV $_.FullName -NTI -Delimiter ";"
}
Explanation:
Uses Get-ChildItem to get all files named *.csv
Iterates through each file with ForEach-Object and uses Import-CSV to load their contents as a PowerShell object
Records the name of the file in $FileName
Uses Select-Object to add a calculated property with the name Filename and the value of the $FileName variable
Uses Export-CSV to write back over the original file. The -NTI (NoTypeInformation) switch is used to ensure the PowerShell object header line is not included.
Mark Wragg's PowerShell code works, but I had to change the delimiter to , instead of ; so that it opens in Excel. I'm trying to figure out how to append the filename to the first column instead of the last because my files don't have the same number of fields so they don't align.

Can I add title rows to a CSV export in Powershell?

I'm exporting a report to a CSV in Powershell and I've been asked to provide three rows above the header row with title information, date of report, etc.
If I export to CSV, i.e. $reportdata | export-csv -notypeinformation fooreport.csv, and then manually add the Title rows in Notepad, i.e.,
"Title"
"Date of Report - XX-XX-XXXX"
"***Important disclaimer about report***"
"Column1","Column2","Column3", etc
"Data1","Data2","Data3", etc
the CSV Report opens fine in Excel and the header and data rows are handled appropriately.
However, when I attempt to do the following in PS,
#Create Report Header
$date = get-date -format "dd MMM yyyy"
$title = #"
"Mailbox Send As, Full Permission and Send on Behalf Report"
"$date"
"***** Important Disclaimer - Only Explicit or Non-Inherited Permissions Are Displayed in this Report *****"
"#
$reportpath = "D:\dev\report.csv"
$rep | export-csv -notypeinformation $reportpath
$temp = gc $reportpath
echo $title > $reportpath
$temp >> $reportpath
the CSV doesn't open properly in Excel and is improperly delimited (i.e., quotes and commas are visible in the cells), even though both files look identical in Notepad.
Is there a way to do what I'm trying to do from Powershell without messing up the integrity of the CSV file? I'm wondering if I'm somehow removing some critical piece of information that signifies a header row in CSV files - since both files are identical, I'm wondering if I"m maybe missing a hidden or special character?
You should not need to output to file with export-csv just to read the file back in to make a change. Lets output the data all at once without reading the back in again.
$rep = $rep | ConvertTo-Csv -NoTypeInformation
$title | Set-Content -Path $reportpath
$rep | Add-Content -Path $reportpath
Not what I intended since I think you could get this in one line but you dont need to read the file back this way. Mostly because it was bugging me I got a one-liner that I was looking for.
$reportpath = "D:\dev\report.csv"
Set-Content -Path c:\temp\export.csv -Value "$title`r`n$(($rep | ConvertTo-Csv -NoTypeInformation) -Join "`r`n")"
ConvertTo-Csv creates a string array. We join all the strings with a newline "`r`n". Then we attach the title to that data and split it up with another newline.