PowerShell script to amend the row in existing CSV - powershell

I'm trying to insert retrieved values into an existing CSV by using -Append -Force. However, it does not place it under the rows that I wanted it to. Anyway to solve this? Currently, the values are in row 12 to 17, but I want it to be in row 2 to 11. Column A to F are in an existing CSV while column G is what I want to input the values into.
Here is my current CSV output
Expected Output that I'm looking for:
And here is my current Powershell code:
Current Powershell code

Related

How to select first column of CSV file without column name in Powershl [duplicate]

This question already has an answer here:
How to read the first column of a CSV file with powershell
(1 answer)
Closed 5 months ago.
Having issues selecting a specific column within a .CSV file in powershell without using the column name. Basically, I have a 5 column spreadsheet where I want to grab the values within the first column but do not want to rely on the column name as it might defer if I use another spreadsheet.
Is there anyway of selecting a column by number or index value and NOT name?
Inspect the properties on the object parsed from the first row by accessing the hidden psobject memberset:
$firstColumnName = $null
Import-Csv whoKnows.csv |ForEach-Object {
if($null -eq $firstColumnName){
# first row, grab the first column name
$firstColumnName = #($_.psobject.Properties)[0].Name
}
# access the value in the given column
$_.$firstColumnName
}

POSH Import-Excel to Export-Csv - Blank Excel Files give unexpected csv output

Working scenario - Excel with Data records / Non-Empty Excel File - Same Code
Excel Input 2 : Excel Input File with records
Csv output 2: Csv output for Excel File with Data records
Not Working Scenario - Excel File with no data records / Empty file - Same Code
Excel Input 1: Blank Input Excel file
Csv Output 1: Csv output file with Issue
I'm using PowerShell to merge multiple excel files into 1, without having to install MSExcel on server. I'm doing that by converting all excel to csv files and then merging all csv files into 1 csv file.
The Excel files row header starts from Row 5. If the Excel files have any rows after row header, the csv file is rendered as expected, but if the Excel file is blank after the row header at Row 5, the csv output file has 3 rows - 1st and 3rd rows have excel row headers and the 2nd row is blank.
$Files = GCI 'E:\SharedExcel\Data Extracts\Project Journals\Temp Server
Downloads\*' | ?{$_.Extension -Match "xlsx?"} | select -ExpandProperty
FullName
ForEach($File in $Files) {
$InFile = Get-Item $File
$OutFile= $InFile.FullName.replace($InFile.Extension,".csv")
Import-Excel $Infile.FullName -StartRow 5 | Export-Csv $OutFile -
NoTypeInformation
}
Expected result : If the Excel File does not have any records after row header at Row #5, the csv should just have row header once with no records. The Actual csv file has 3 rows- 1st and 3rd row have row headers and 2nd row is blank.
Successful Powershell script execution Output My script runs successfully producing the 4 newly created csv files from the 4 excel files.
From the docs:
.PARAMETER StartRow
The row from where we start to import data, all rows above the StartRow are disregarded. By default this is the first row.
When the parameters ‘-NoHeader’ and ‘-HeaderName’ are not provided, this row will contain the column headers that will be used as property names. When one of both parameters are provided, the property names are automatically created and this row will be treated as a regular row containing data.
Without an additional parameter set, the referenced row will be treated as property names. I think you are passing a list of property names with the values equal to the property names to the Export-Csv cmdlet.

Extract specific values from cells from a CSV

I have to combine a lot of files , mostly CSV, already have code to combine however I need first to trim the desired csv files so I can get the data that I want. Each CSV has first 2 columns of 8 rows which contain data that I want. and then just below those there is a row that generates 8 columns. I am only having issue grabbing data from the first 8 rows of the 2 columns.
Example of the csv first 3 rows:
Target Name: MIAW
Target OS: Windows
Last Updated On: June 27 2019 07:35:11
This is the data that I want, the first 3 rows are like this, with 2 columns. My idea is to store the 3 values of the 2nd column each into a variable and then use it with the rest of my code.
As I only have a problem extracting the data, and since the way the CSV are formated there is no header at all, it is hard to come up with an easy way to read the 2nd column data. Below is an example, this of course will be used to process several files so it will be a foreach, but I want to come up first with the simple code for 1 file so I can adapt it myself to a foreach.
$a = MIAW-Results-20190627T203644Z.csv
Write-Host "$a[1].col2"
This would work if and only if I had a header called col2, I could name it with the first value on the 2nd column but the issue is that that value will change for CSV file. So the code I tried would not work for example if I were to import several files using:
$InFiles = Get-ChildItem -Path $PSScriptRoot\ *.csv -File |
Where-Object Name -like *results*
Each CSV will have a different value for the first value on the 2nd column.
Is there an easier way to just grab the 3 rows of the second column that I need? I need to grab each one and store each in a different variable.

excel to matlab structure using column names

I have to analyze a data given in Excel format. I will use MATLAB and I want to write a code which automatically creates structure using the column's name.
The columns are formatted as follows:
Speed_55m.max Speed_55m.min Speed_55m.stdev Speed_55m.value
And that 4 pair of names is repeating for different heights.I want to have a loop which reeds the column names and creates a structure.
I have tried the following code:
[a,b]=xlsread('PP_RR.xlsx');
for icol=1:size(a,2)
char(b{icol})=a(:,icol);
end
But I received the following error:
Subscripted assignment dimension mismatch.
A workaround is using the file explorer window on the MATLAB home page. Double click on the excel spreadsheet to open the import wizard, select "import as table". MATLAB will automatically create a 'table' data type variable with the same column names as your spreadsheet. If that does not work, convert the .xlsx to a .csv, and it will for sure.

Prevent thousand separator in TSQL export to CSV

When exporting a TSQL select result to CSV the values show a strange thousandseparator. Partial code is:
CONVERT(DECIMAL(10,2),i.UserNumber_04) as CAP
The query results show perfect values, for example: 1470.00 but the CSV or txt file show strange values like 1,470,00. How can I prevent the first comma?
At first I thought it was just the formatting style in excel, but it does the same in txt files.