How to Hide/Remove the header from output csv via Powershell script

Felix JIN

I am using Psping to check the latency of IPs with port and export the result CSV without display the header.

I have tried -select skip 1 it seems not working but with errors.


function CheckLatency 
{
  param([string[]]$servers)
  foreach ($server in $servers) 
  {
    $times = [ordered]@{ Server = "$server";TimeStamp = (Get-Date -f "yyyy-MM-dd hh:mm:ss"); Minimum = 0; Maximum = 0; Average = 0; } 
    $results = & "c:\users\test\desktop\psping.exe" -n 1 $server 2>&1 | select-string "Minimum" 
    if ($results) {
      $results = $results.tostring() -split "," 
      foreach ($result in $results) 
      {
        $result = ($result -replace "ms","").trim() 
        $parsed = $result -split " "
        switch ($parsed[0]) 
        {
          "Minimum" {$times.Minimum = $parsed[2]}
          "Maximum" {$times.Maximum = $parsed[2]}
          "Average" {$times.Average = $parsed[2]}
        }
      }
      new-object -type PSObject -prop $times
    }
  }
}

$csvFile = "C:\users\test\desktop\check$(get-date -f yyyy-MM-dd-hhmmss).csv"
CheckLatency 8.8.8.8:443,8.8.8.8:80 | Export-CSV -LiteralPath $csvFile -NoTypeInformation

Output with header

Please help remove the header

mklement0

Export-Csv invariably includes a header row (the list of property names of the output objects).

Your only option is to remove that row after the fact, using plain-text processing:

$csvFile = "C:\users\test\desktop\check$(get-date -f yyyy-MM-dd-hhmmss).csv"

# ... your code that calls 
# Export-CSV -LiteralPath $csvFile -NoTypeInformation

# Read the resulting file as an array of lines, skip the 1st line,
# and write the result back to the file.
(Get-Content -LiteralPath $csvFile) | Select-Object -Skip 1 | Set-Content -LiteralPath $csvFile

Note the (...) around the Get-Content call, which ensures that the file is read in full before its lines are sent through the pipeline, allowing the input file to be rewritten in the same pipeline.
Do note that there's a slight chance of the file getting corrupted if the write process is interrupted.

Note that Windows PowerShell uses ANSI encoding by default with Set-Content, whereas Export-Csv uses ASCII; use -Encoding as needed. PowerShell Core, fortunately, defaults to BOM-less UTF-8 consistently.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass output from a PowerShell cmdlet to a script?

Powershell Script to parse selected information from txt file and output to csv

Powershell Script from CSV

How to run node js script from powershell script and use the output of node js script in powershell script?

How to Combining PowerShell Output from multiple server to Single csv

How to get output from powershell script over ssh?

How to create a variable from specific part of command output in powershell script

CSV export from a powershell script

In powershell how to output an ArrayList to CSV

Powershell: how to retrieve powershell commands from a csv and execute one by one, then output the result to the new csv

How to download CSV file from PHP script via AJAX?

How to use output variable value from one powershell script to another powershell script

Sending output via Email from Powershell

Removing the header in Powershell Export-Csv output for Select-Object

How to add header to a CSV file initially with PowerShell

Powershell: Output a search from a csv to a small gui

To create XML output file from PowerShell script

Running a powershell script from ansible and register output

How to read a column without header from csv and save the output in a txt file using Python?

How to extract one specific column (no header, say column 2) from multiple csv files using Powershell?

Line Removal from .CSV via Batch Script

How to get the samAccountName from a csv file exported from Teams via powershell

How to make Powershell to output the result to a csv

How to filter the output from the output of this powershell command?

How to run a PowerShell script with verbose output?

How to change powershell script output behavior

How to send powershell script output to printer with formatting?

How can the output of an existing PowerShell script be displayed?

How to run R script remotely via Powershell