Format text file as a table using PowerShell - powershell

I have a text file C:\file.txt with this formatting:
VersionID|VersionNumber|UpgradeDate|Comments
---------|-------------|-----------|--------
1156|3.3.0|2017-01-04 23:13:04.687|3.3 comment
I want to remove the ---------| and output in table format.
VersionID|VersionNumber|UpgradeDate |Comments
1156 |3.3.0 |2017-01-04 23:13:04.687|3.3 comment
The output would then be stored in a text file say C:\output.txt
Does anyone know how I can do that?

This will do what you requested, import the text file then export is as a "table" format. It exports as CSV. If you need it to be exported as a different format, please be more clear in your question. Thanks!
import-csv C:\file.txt -Delimiter "|" | export-csv C:\Output.csv

Related

PowerShell and CSV: Stop CSV from turning text data into Scientific Notation

I have a CSV column with alpha numerical combinations in a column.
I am later going to use this csv file in a PowerShell script by importing the data.
Examples: 1A01, 1C66, 1E53.
Now before putting these values in, I made sure to format the column as text.
Now at first it works. I input the data, save. I test in PowerShell to import it and
all data shows up valid including 1E53. But lets say I edit the file again later to add data and then save and close. I re-import into PowerShell and 1E53 comes in as 1.00E+53. How can I prevent this permanently? Note that the column is filled with codes and there are lots of #E##.
Your issue is not with PowerShell, its with Excel. For a demonstration, take 1E53 and enter it into Excel and then save that excel file as a CSV file. You will see that the value is now changed to 1.00E+53.
How to fix this?
There are a few ways of disabling scientific notation:
https://superuser.com/questions/452832/turn-off-scientific-notation-in-excel
https://www.logicbroker.com/excel-scientific-notation-disable-prevent/
I hope some of them work for you.
I think you can rename the file to .txt instead of .csv and excel may treat it differently.
Good Luck
As commented:
You will probably load the csv from file:
$csv = Import-Csv -Path 'X:\original.csv' -UseCulture
The code below uses a dummy csv in a Here-String here:
$csv = #'
"Column1","Column2","ValueThatMatters"
"Something","SomethingElse","1E53"
"AnotherItem","Whatever","4E12"
'# | ConvertFrom-Csv
# in order to make Excel see the values as Text and not convert them into scientific numbers
$csv | ForEach-Object {
# add a TAB character in front of the values in the column
$_.ValueThatMatters = "`t{0}" -f $_.ValueThatMatters
}
$csv | Export-Csv -Path 'X:\ExcelFriendly.csv' -UseCulture -NoTypeInformation

Powershell Converting Tab Delimited CSV to Comma delimited CSV without Quotes

We get a tab delimited CSV from COGNOS External system in a public folder. This fails to upload to Salesforce via Dataloader CLI.
com.salesforce.dataloader.exception.DataAccessRowException: Error
reading row #0: the number of data columns (98) exceeds the number of
columns in the header (97)
But if you open the csv in MS Excel, and save as a new CSV (UTF-8) and then pass it to data loader CLI it works without any issue.
The difference in EXCEL converted file seems to be it's Comma separated instead of Tab.
Then I tried to convert Original Tab Delimited CSV to Comma separated CSV using below command,
import-csv source.csv -delimiter "`t" | export-csv target.csv -notype
But the output of this has quotes, Data Loader now runs with the File, but imports nothing into Salesforce, it seems it's not able to identify field-names properly.
Then I tried below command to remove the double quotes,
import-csv source.csv -delimiter "`t" | export-csv target.csv -notype
(Get-Content target.csv) | Foreach-Object {$_ -replace '"', ''}|Out-File target.csv
But this resulted in an Index out of range error, which is not clear.
What would be the best approach to do this conversion for Data Loader CLI?
What can make this conversion same as EXCEL's conversion?
Highly appreciate Any suggestions, thoughts, help to achieve this.
Thanks!
SalesForce has strict rules for CSV files. Also, on this page it says that no more than 50000 records can be imported at one time.
Main thing here is that the file MUST be in UTF8 format.
The quotes around the values are needed.
This should do it (provided you do not have more than 50000 records in the Csv):
Import-Csv -Path 'source.csv' -Delimiter "`t" | Export-Csv -Path 'target.csv' -Encoding UTF8 -NoTypeInformation
(source.csv is the TAB-delimited file you receive from COGNOS)

how to convert from Newline Delimited text file to csv with Powershell

I have a text file containing the below test data:
1234
\\test
QATest
Silk
Chrome
I have a requirement to convert this text file into a CSV file using Powershell which would look somewhat like
1234,\\test,QATest,Silk,Chrome
Could anybody please suggest the right way?
Read all the lines in, then concatenate them using the -join operator:
$originalLines = Get-Content .\input.txt
$originalLines -join ',' |Set-Content .\output.csv

Join a text in the same field csv powershell

$ColumnsToRemove | ForEach-Object{
[void]$sheet.Cells.Item(1,$_).EntireColumn.Delete();
$sheet.Cells.Item($_,1) = -join '0044';
}
I am trying to join the number '0044' in the first column of a csv after deleting a few columns.
Could you please help me on that?
If you've got a CSV file try to use Import-CSV and Export-Csv CmdLets to handle your file, it will be easier than Excel.
If you give an example of your CSV file, I can help you.

Reducing one of csv columns contents down single character with powershell

I have a csv file with headers as follows
physicalDeliveryOfficeName,sn,middleName,givenName,info,Company,employeeID,Description
And I am wanting to change the contents of the middleName column down to just the first character then save it out as another csv file with all of the other columns unchanged.
Im not sure where to start with this.
The csv file is over 12000 rows and Im wanting to do it the most efficient way with powershell.
I am new to using Powershell so advise is greatly appreciated.
You should show some effort. A couple of google searches would go a long way. Here's one way:
Import-CSV myfile.csv |
Foreach-Object {
$_.middleName = $_.middleName.Substring(0,1)
$_
} |
Select-Object physicalDeliveryOfficeName,sn,middleName,givenName,info,Company,employeeID,Description |
Export-CSV myupdatedfile.csv -NoTypeInformation