How to Get the First and Last Line of a CSV File in PowerShell?

Today in a PowerShell training session, someone asked how to get the first and last line of a CSV file in PowerShell. It is quite useful and an interesting topic. In this tutorial, I will explain how to get the first and last line of a CSV file in PowerShell.

To get the first and last lines of a CSV file in PowerShell, you can use the Import-Csv cmdlet combined with Select-Object. First, read the CSV file into a variable using $csvData = Import-Csv -Path “C:\MyFolder\file.csv”. Then, retrieve the first line with $firstLine = $csvData | Select-Object -First 1 and the last line with $lastLine = $csvData | Select-Object -Last 1.

Get the First and Last Line of a CSV File in PowerShell

Let me show you how to get the first and last lines of a CSV file in PowerShell using various methods with examples.

Method 1: Using Import-Csv and Select-Object

One of the simplest ways to get the first and last lines of a CSV file in PowerShell is by using the Import-Csv cmdlet combined with Select-Object. Here’s how you can do it:

Syntax:

Here is the syntax:

$csvData = Import-Csv -Path "C:\MyFolder\file.csv"
$firstLine = $csvData | Select-Object -First 1
$lastLine = $csvData | Select-Object -Last 1

Here:

  • Import-Csv: Reads the CSV file and converts it into a collection of custom objects.
  • Select-Object -First 1: Selects the first object from the collection.
  • Select-Object -Last 1: Selects the last object from the collection.

Example:

Now, let me show you an example.

Let’s say you have a CSV file named employees.csv with the following content:

Name,Department,Location
John Doe,Engineering,New York
Jane Smith,Marketing,San Francisco
Michael Brown,Sales,Chicago

You can also download this CSV file.

To get the first and last lines, you would use the below PowerShell script.

$csvData = Import-Csv -Path "C:\MyFolder\employees.csv"
$firstLine = $csvData | Select-Object -First 1
$lastLine = $csvData | Select-Object -Last 1

Write-Output "First Line: $($firstLine.Name), $($firstLine.Department), $($firstLine.Location)"
Write-Output "Last Line: $($lastLine.Name), $($lastLine.Department), $($lastLine.Location)"

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

powershell read first line of csv file

Check out Convert CSV to HTML Table in PowerShell

Method 2: Using Get-Content with Select-Object

Now, let me show you another method to get the first and last line of a CSV file.

You can use the Get-Content cmdlet, which reads the file as an array of strings. This method is particularly useful if you only need the raw text lines.

Syntax:

Here is the syntax:

$lines = Get-Content -Path "C:\MyFolder\file.csv"
$firstLine = $lines | Select-Object -First 1
$lastLine = $lines | Select-Object -Last 1

Here:

  • Get-Content: Reads the content of the file and returns it as an array of strings.
  • Select-Object -First 1: Selects the first line from the array.
  • Select-Object -Last 1: Selects the last line from the array.

Example:

Let me show you an example.

We will also use the same employees.csv file above. Below is the complete PowerShell script.

$lines = Get-Content -Path "C:\MyFolder\employees.csv"
$firstLine = $lines | Select-Object -First 1
$lastLine = $lines | Select-Object -Last 1

Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"

Here is the exact output in the screenshot below:

powershell get last line of csv file

Check out Export Table to CSV in PowerShell

Method 3: Using Array Indexing

For those who prefer a more direct approach, you can read the file into an array and then access the first and last elements using array indexing.

Syntax:

$lines = Get-Content -Path "C:\MyFolder\file.csv"
$firstLine = $lines[0]
$lastLine = $lines[-1]
  • Get-Content: Reads the file into an array of strings.
  • $lines[0]: Accesses the first element of the array.
  • $lines[-1]: Accesses the last element of the array.

Example:

Now, let me show you an example.

Again, using employees.csv:

$lines = Get-Content -Path "C:\MyFolder\employees.csv"
$firstLine = $lines[0]
$lastLine = $lines[-1]

Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"

Here is the exact output you can see in the screenshot below:

Get the First and Last Line of a CSV File in PowerShell

Conclusion

In this tutorial, I explained how to access the first and last lines of a CSV file in PowerShell using various methods, like using Import-Csv, Get-Content, etc. For each method, I have also explained real examples.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.