One of my team members struggled to write a script to find the first and last lines of a multiple-line string. I tried various methods. In this tutorial, I have explained how to get the first and last lines of a multiline string in PowerShell.
To get the first and last line of a multiline string in PowerShell, you can use the -split operator to convert the string into an array of lines and then access the first and last elements. For example:
$multilineString = @"
First line
Second line
Last line
"@
$lines = $multilineString -split "`n"
$firstLine = $lines[0]
$lastLine = $lines[-1]Let us try different methods.
Method 1: Using Get-Content
The Get-Content cmdlet in PowerShell is used to read the content of files. We can also use the same cmdlet to process multiline strings using the -Raw parameter.
Here is an example of how to use the Get-Content cmdlet in PowerShell to get the first and last name of a multiline string.
# Define a multiline string
$multilineString = @"
First line
Second line
Third line
Fourth line
Last line
"@
# Convert the multiline string into an array of lines
$lines = $multilineString -split "`n"
# Get the first and last lines
$firstLine = $lines[0]
$lastLine = $lines[-1]
# Output the results
Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"In this example, the -split operator is used to split the multiline string into an array of lines. The first line is accessed using $lines[0], and the last line using $lines[-1].
I have executed the above PowerShell script, and you can see the output in the screenshot below:

Read Convert Multiline String to Single Line in PowerShell
Method 2: Using Select-Object
The Select-Object cmdlet in PowerShell can be used to select specific properties of objects, including lines of text. Here is another method to get the first and last lines of a PowerShell multiline string using the Select-Object.
Here is the full script:
# Define a multiline string
$multilineString = @"
First line
Second line
Third line
Fourth line
Last line
"@
# Convert the multiline string into an array of lines
$lines = $multilineString -split "`n"
# Use Select-Object to get the first and last lines
$firstLine = $lines | Select-Object -First 1
$lastLine = $lines | Select-Object -Last 1
# Output the results
Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"Here, Select-Object is used with the -First and -Last parameters to retrieve the first and last lines, respectively.
You can see the output in the screenshot below:

Method 3: Using Get-Content with -TotalCount and -Tail
Here is the third method.
If you are working with files, Get-Content can be used with the -TotalCount parameter to get the first few lines and the -Tail parameter to get the last few lines.
Here is an example.
# Save the multiline string to a temporary file
$multilineString = @"
First line
Second line
Third line
Fourth line
Last line
"@
$tempFile = "tempfile.txt"
$multilineString | Out-File -FilePath $tempFile
# Get the first and last lines from the file
$firstLine = Get-Content -Path $tempFile -TotalCount 1
$lastLine = Get-Content -Path $tempFile -Tail 1
# Output the results
Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"
# Clean up the temporary file
Remove-Item -Path $tempFileIn this example, the multiline string is first saved to a temporary file. Get-Content is then used with -TotalCount to get the first line and -Tail to get the last line. Finally, the temporary file is removed.
Read Filter Strings in PowerShell
Method 4: Using Regular Expressions
You can also use regular expressions to extract the first and last lines from a multiline string in PowerShell.
Here is an example:
# Define a multiline string
$multilineString = @"
First line
Second line
Third line
Fourth line
Last line
"@
# Use a regex pattern to match the first and last lines
if ($multilineString -match "^(.*?)[\r\n]+.*[\r\n]+(.*?)$") {
$firstLine = $matches[1]
$lastLine = $matches[2]
}
# Output the results
Write-Output "First Line: $firstLine"
Write-Output "Last Line: $lastLine"In this example, a regular expression matches the first and last lines of the multiline string. The -match operator captures these lines into the $matches array.
Here is the output of the above PowerShell script in the below screenshot:

Conclusion
In this tutorial, I have explained how to get the first and last lines of a multiline string in PowerShell using various methods.
- Using
Get-Contentand-split: For in-memory strings. - Using
Select-Object: Useful for selecting specific lines from an array. - Using
Get-Contentwith-TotalCountand-Tail: Ideal for file-based operations. - Using Regular Expressions: Powerful for complex string manipulations.
You may also like the following tutorials:
- Get Length of a String in PowerShell
- Case Insensitive Strings Comparison in PowerShell
- Compare Strings in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.