String Starts With in PowerShell: Simple Methods That Works

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:

powershell string starts with

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.

powershell string starts with case insensitive

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:

powershell string starts with substring

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:

powershell string starts with regex

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:

MethodRelative PerformanceCase-Sensitive by DefaultBest For
StartsWith()FastestYesLarge datasets, performance-critical code
-likeMediumNoPowerShell-native scripts, readability
-matchSlowestNoComplex 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 -like or -match operators, 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 -like or -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:

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Live Webinar

Quiz App Using SharePoint Framework (SPFx)

Learn to built a complete Quiz Management solution that enables admins to create and manage quizzes, categories, questions, and settings with an easy automated setup process in SharePoint. It also includes an interactive quiz experience for users and a powerful dashboard to track participation, analyze results, and view detailed performance reports with charts and answer insights.

📅 2nd June 2026 – 10:00 AM EST | 7:30 PM IST

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App