Recently, I was working on a script where I needed to check if strings began with certain characters. There are various methods available to do this in PowerShell.
In this article, I’ll explain various methods for checking if a string starts with specific characters in PowerShell. You can use these techniques in various scenarios, such as filtering log files, processing user input, or cleaning data.
Here are the methods with examples.
Method 1 – Using the StartsWith() Method
The most direct way to check if a string starts with specific characters is to use the built-in StartsWith() method in PowerShell. This method is part of the String class in .NET, which PowerShell leverages.
Here’s how you can use it:
$text = "PowerShell is awesome"
$result = $text.StartsWith("Power")
Write-Host "Does the text start with 'Power'? $result"
The StartsWith() method returns a boolean value – $true if the string starts with the specified prefix and $false if it doesn’t.
Here is the exact output in the screenshot below:

You can also make the comparison case-insensitive:
$text = "PowerShell is awesome"
$result = $text.StartsWith("power", [StringComparison]::OrdinalIgnoreCase)
Write-Host "Does the text start with 'power' (case-insensitive)? $result"
This method is straightforward and perfect for single string comparisons. Here is the exact output in the screenshot below, after I executed the above PowerShell script using VS Code.

Check out PowerShell Array of Strings
Method 2 – Using the -like Operator
PowerShell’s -like operator provides a more PowerShell-native way to check string prefixes. Here is an example.
$text = "PowerShell is awesome"
if ($text -like "Power*") {
Write-Host "Text starts with 'Power'"
} else {
Write-Host "Text does not start with 'Power'"
}
The asterisk (*) is a wildcard that matches any number of characters. You can see the exact output in the screenshot below:

For case-insensitive matching, use -ilike instead:
$text = "PowerShell is awesome"
$result = $text -ilike "power*"
Write-Host "Does the text start with 'power' (case-insensitive)? $result"
I often use this approach when writing quick scripts, as it feels more natural in the PowerShell ecosystem.
Check out Replace Carriage Returns in Strings Using PowerShell
Method 3 – Using Regular Expressions with -match
When you need more complex pattern matching, regular expressions with the -match operator are your friend:
$text = "PowerShell is awesome"
$result = $text -match "^Power"
Write-Host "Does the text start with 'Power'? $result"
The caret symbol (^) in the regular expression indicates the start of the string. For case-insensitive matching:
$text = "PowerShell is awesome"
$result = $text -imatch "^power"
Write-Host "Does the text start with 'power' (case-insensitive)? $result"
This method is particularly useful when your prefix matching needs are part of a larger pattern-matching requirement.
You can see the exact output in the screenshot below:

Read Split a String by Length in PowerShell
Method 4 – Filtering Collections of Strings
One of the most common use cases for checking string prefixes is filtering collections. Here’s how to filter an array of strings to only include those that start with a specific prefix:
$files = @("Report-2025.xlsx", "Summary-2025.docx", "Report-2024.xlsx", "Presentation.pptx")
# Using Where-Object with StartsWith method
$reportFiles = $files | Where-Object { $_.StartsWith("Report") }
# Using Where-Object with -like operator
$reportFiles = $files | Where-Object { $_ -like "Report*" }
# Using Where-Object with -match operator
$reportFiles = $files | Where-Object { $_ -match "^Report" }
Write-Host "Files that start with 'Report': $($reportFiles -join ', ')"
All three approaches work well, but I tend to use the -like operator for simple cases and the StartsWith() method when working with large datasets for performance reasons.
Check out PowerShell Substring() Example
Method 5 – Working with File Systems
A practical application of string prefix checking is when working with files. Here’s how you can find all PowerShell script files in a directory:
# Get all .ps1 files that start with 'Backup'
$scriptFiles = Get-ChildItem -Path "C:\Scripts" -Filter "Backup*.ps1"
foreach ($file in $scriptFiles) {
Write-Host "Found backup script: $($file.Name)"
}
Using the -Filter parameter with wildcards is much more efficient than retrieving all files and then filtering them with Where-Object.
Performance Comparison
When working with large datasets, performance becomes a consideration. I’ve found that different methods have varying performance characteristics:
| Method | Relative Performance | Case-Sensitive by Default | Best For |
|---|---|---|---|
| StartsWith() | Fastest | Yes | Large datasets, performance-critical code |
| -like | Medium | No | PowerShell-native scripts, readability |
| -match | Slowest | No | Complex pattern matching needs |
For a quick demonstration, let’s compare the methods with a simple benchmark:
$testString = "PowerShell is powerful"
$iterations = 100000
$sw = [System.Diagnostics.Stopwatch]::StartNew()
for ($i = 0; $i -lt $iterations; $i++) {
$result = $testString.StartsWith("Power")
}
$sw.Stop()
Write-Host "StartsWith() method: $($sw.ElapsedMilliseconds) ms"
$sw = [System.Diagnostics.Stopwatch]::StartNew()
for ($i = 0; $i -lt $iterations; $i++) {
$result = $testString -like "Power*"
}
$sw.Stop()
Write-Host "-like operator: $($sw.ElapsedMilliseconds) ms"
$sw = [System.Diagnostics.Stopwatch]::StartNew()
for ($i = 0; $i -lt $iterations; $i++) {
$result = $testString -match "^Power"
}
$sw.Stop()
Write-Host "-match operator: $($sw.ElapsedMilliseconds) ms"
Check out Convert String to Integer in PowerShell
Real-World Example: Processing Log Files
Let’s see a practical example of using string prefix checking to analyze log files:
# Assuming we have log files with entries like:
# ERROR: Connection failed
# INFO: User logged in
# WARNING: Low disk space
$logEntries = Get-Content -Path "C:\Logs\application.log"
$errorCount = ($logEntries | Where-Object { $_.StartsWith("ERROR:") }).Count
$warningCount = ($logEntries | Where-Object { $_.StartsWith("WARNING:") }).Count
$infoCount = ($logEntries | Where-Object { $_.StartsWith("INFO:") }).Count
Write-Host "Log Analysis:"
Write-Host "- Errors: $errorCount"
Write-Host "- Warnings: $warningCount"
Write-Host "- Info messages: $infoCount"
This simple script can quickly give you an overview of your log file contents based on message type prefixes.
Check out Convert Multiline String to Array in PowerShell
Common Issues and Fixes
Based on my experience, here are some common issues to watch out for:
- Whitespace: Be careful with leading spaces or tabs when checking prefixes. You might need to use
Trim()first.
$text = " PowerShell"
$result = $text.Trim().StartsWith("Power")
- Empty strings: Always check if your string is null or empty before performing operations on it.
if (![string]::IsNullOrEmpty($text) -and $text.StartsWith("Power")) {
# Do something
}
- Special characters: When using the
-likeor-matchoperators, be aware that some characters have special meanings and might need to be escaped. - Performance in loops: For operations inside tight loops, the
StartsWith()method typically performs better than-likeor-match.
PowerShell provides multiple ways to check if a string starts with specific characters.
I’ve found that the StartsWith() method works best for most scenarios due to its performance. However, the -like operator often feels more natural in PowerShell scripts, especially for simple cases.
Other PowerShell tutorials you may also like:
- Replace a String in Text File with PowerShell
- Convert String to DateTime in PowerShell
- Replace String in XML File using PowerShell
- Replace String In JSON File Using PowerShell

Hey! I’m Bijay Kumar, founder of SPGuides.com and a Microsoft Business Applications MVP (Power Automate, Power Apps). I launched this site in 2020 because I truly enjoy working with SharePoint, Power Platform, and SharePoint Framework (SPFx), and wanted to share that passion through step-by-step tutorials, guides, and training videos. My mission is to help you learn these technologies so you can utilize SharePoint, enhance productivity, and potentially build business solutions along the way.