Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to skip the first line in CSV file

Tags:

powershell

csv

I want to skip the Name,Path which is the first line when it generates the CSV file. I dont know how to do so and I have no idea how to do so .

here is my code:

function Get-Path{
    param($Object)

    $path = $object.Name
    $parent = Get-View $Object.ExtensionData.ResourcePool
    while($parent){
    $path = $parent.Name + "/" + $path

        if($parent.Parent){
            $parent = Get-View $parent.Parent
        }
        else{$parent = $null}
    }
    $path
}

Get-VM | Select Name,
@{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources"}} | Export-csv C:\izaz\test.csv -NoTypeInformation
like image 818
Izaz Avatar asked Dec 19 '22 02:12

Izaz


2 Answers

The easiest way to export a CSV file with no header is to use ConvertTo-Csv and then use Select-Object to skip the first line:

Get-VM `
| Select Name, @{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources"}} `
| ConvertTo-Csv -NoTypeInformation `
| Select-Object -Skip 1 `
| Set-Content -Path C:\izaz\test.csv
like image 133
Bacon Bits Avatar answered Feb 20 '23 15:02

Bacon Bits


Import-CSV -Header $StringWithAlternateHeader will set the header for the array you are importing the CSV into. It does NOT REPLACE any existing header. If you do not remove the old header from the CSV prior to import, then the old header will become a record in the array.

Example CSV:
Name,Age
Sally,15
Paul,14

$Array = Import-CSV .\example.csv
ARRAY[0] is Sally,15
ARRAY[1] is Paul,14

$Array = Import-CSV .\example.csv -Header @("Child","Age")
ARRAY[0] is Name,Age
ARRAY[1] is Sally,15
ARRAY[2] is Paul,14
like image 42
TheCarefulCoder Avatar answered Feb 20 '23 15:02

TheCarefulCoder