Check If String Starts With in PowerShell

Checking if a string starts with some character (or a string) is a common need for every kind of PowerShell script. We can use PowerShell’s like operator with wildcard characters to check if a string starts with both case-sensitive and case-insensitive.

The following method is used to check if a string starts with another string using the like operator. By default like operator ignore the case-sensitive check.

$strVal ='Hello world'

if($strVal -like 'hello*') {
      Write-Host 'Your string is start with hello'
} else {
      Write-Host 'Your string does not start with hello"'

To perform a Case-Sensitive comparison just prefix the word “c” with the like operator (“clike”).

$strVal ='Hello world'

if($strVal -clike 'Hello*') {
      Write-Host 'True'
} else {
      Write-Host 'False'
}
PowerShell

Comments