Check If String Starts With in PowerShell using .NET Extension

We can use the .NET’s string extension function StartsWith to check whether a string starts with a set of characters.

The following method is used to check if a string starts with another string.

$strVal ='Hello world'
if($strVal.StartsWith('Hello')) {
     Write-Host 'True'
} else {
     Write-Host 'False'
}

Use the following method if you want to ignore the case in the start with check.

$strVal ='Hello world'
if($strVal.StartsWith('hello','CurrentCultureIgnoreCase')) {
     Write-Host 'True'
} else {
     Write-Host 'False'
}
PowerShell

Comments

Leave a Comment

All fields are required. Your email address will not be published.