Reputation: 85
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
Upvotes: 5
Views: 30349
Reputation: 23633
From PowerShell version 7.4, ConvertTo-Csv
and Export-Csv
will have a -NoHeader
parameter:
Get-VM |
Select Name, @{N="Path";E={(Get-Path -Object $_) -replace "^.*?Resources"}} |
Export-Csv -NoHeader -NoTypeInformation
See also: #17527
Add -NoHeader
switch to Export-Csv
and ConvertTo-Csv
Upvotes: 0
Reputation: 1
You could use a simple IF statement to get rid of your header line.
$Array = Import-CSV .\example.csv -Header @("Child","Age")
if ($Array.Child -ne "Child)" {
ARRAY[1] is Sally,15
ARRAY[2] is Paul,14
}
Upvotes: 0
Reputation: 32145
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
Upvotes: 9
Reputation: 31
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
Upvotes: 3
Reputation: 18940
If you plan on using the csv file with Import-csv, look at the -header parameter in the cmdlet documentation. This parameter allows you to specify alternate column names instead of the ones stored in the csv file.
This is an alternative approach to the accepted answer.
Upvotes: 0