Recently, I got a requirement to find lines starting with a specific prefix using PowerShell. I tried different methods. In this tutorial, I will show you how to find lines starting with a specific string in PowerShell.
To find lines starting with a specific string in PowerShell, you can use the Select-String cmdlet with a regular expression pattern. For example, to find lines starting with “New York” in a file named cities.txt, use the command Select-String -Path “cities.txt” -Pattern “^New York”. The ^ character ensures that only lines beginning with “New York” are matched, making this method efficient for searching through text files.
Now, let me show you how to find lines starting with a specific string in PowerShell using various methods.
1. Using Select-String
The Select-String cmdlet in PowerShell is similar to the Unix command grep. It allows you to search for text patterns within files or strings.
Now, let us see how to use Select-String with an example.
Example: Find Lines Starting with “New York”
Suppose you have a text file containing a list of city names, and you want to find all lines that start with “New York”. Here is how you can do it using Select-String:
# Create a sample text file with city names
$cityNames = @"
New York City
Los Angeles
New York State
Chicago
Houston
New York County
Phoenix
"@
# Save the text to a file
$cityNames | Out-File -FilePath "C:\MyNewFolder\cities.txt"
# Use Select-String to find lines starting with "New York"
Select-String -Path "cities.txt" -Pattern "^New York"In this example, the ^ character is a regular expression symbol that matches the start of a line. The Select-String cmdlet will output all lines from the file cities.txt that begin with “New York”.
You can see the output in the screenshot below after I executed the above PowerShell script.

Check out Replace Multiple Strings in an Array Using PowerShell
2. Using the -match Operator
Now, let me show you another method to find lines starting with a specific string is by using the -match operator. This operator supports regular expressions and can be used to filter lines in a string array.
Example: Find Lines Starting with “California”
Consider a list of state names, and you want to find all lines starting with “California”. Here is the complete PowerShell script.
# Create an array of state names
$stateNames = @(
"California",
"Texas",
"California Republic",
"Florida",
"New York",
"California City"
)
# Use the -match operator to filter lines starting with "California"
$californiaStates = $stateNames | Where-Object { $_ -match "^California" }
# Output the result
$californiaStatesThis script will output:
California
California Republic
California CityYou can see the exact output in the screenshot below after I executed the above script.

Read Extract Strings Between Parentheses in PowerShell
3. Using the StartsWith Method
If you want to get the lines starting with a particular string without regular expressions, then you can use the StartsWith method of the string object. This method checks if a string starts with a specified prefix.
Example: Find Lines Starting with “Washington”
Let’s say you have a list of state capitals and want to find all lines starting with “Washington”:
# Create an array of state capitals
$stateCapitals = @(
"Washington, D.C.",
"Sacramento, California",
"Austin, Texas",
"Washington, Seattle",
"Albany, New York"
)
# Use the StartsWith method to filter lines starting with "Washington"
$washingtonCapitals = $stateCapitals | Where-Object { $_.StartsWith("Washington") }
# Output the result
$washingtonCapitalsThis will output:
Washington, D.C.
Washington, SeattleNow, you can see the output in the screenshot below after I executed the above PowerShell script.

Conclusion
PowerShell provides several methods to find lines starting with a specific string. I have explained each method with examples. The StartsWith method is easy to use, and it is mostly used in PowerShell. The Select-String cmdlet is used with regular expressions.
Here, I have explained how to find lines starting with a specific string using PowerShell.
You may like the following tutorials:
- Get Length of a String in PowerShell
- Encode and Decode Strings in PowerShell
- Case Insensitive Strings Comparison 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.