Join a text in the same field csv powershell - 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.

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

Filtering data from CSV file with PowerShell

I have huge csv file where first line contains headers of the data. Because the file size I can't open it with excel or similar. I need to filter rows what I only need. I would want to create new csv file which contains only data where Header3 = "TextHere". Everything else is filtered away.
I have tried in PowerShell Get-Content Select-String | Out-File 'newfile.csv' but it lost header row and also messed up with the data putting data in to wrong fields. There is included empty fields in the data and I believe that is messing it. When I tried Get-Content -First or -Last data seemed to be in order.
I have no experience handling big data files or powershell before. Also other options besides PowerShell is also possible if it is free to use as "non-commercial use"
try like this (modify your delimiter if necessary):
import-csv "c:\temp\yourfile.csv" -delimiter ";" | where Header3 -eq "TextHere" | export-csv "c:\temp\result.csv" -delimiter ";" -notype

Format text file as a table using 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

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

Write output data row to pipe delineated text file in powershell

I am a powershell newbie and I need a script that does a write output to a delineated text file in powershell.
In sequence, this is what I would like to do in powershell:
read data from excel file and store to variables
read data from table in mssql
write output the each row from (2) to a text file (pipe delineated) appending the value from (1)
I was able to figure out sequence 1 and 2 however I am stumped on (3).
Here is a snippet of what i am trying to do:
# Iterate through the dataset
foreach ($row in $ds.tables["location"].rows)
{
Out-file ?
}
Please help!
$myData | Export-CSV -delimiter '|' -Path $MyFileName
Note, the Export-CSV cmdlet will handle looping over the data, so no need for your own loop.