Check the Beginning of a String Using PowerShell

Marion Paul Kenneth Mendoza Jan 30, 2023 Jan 23, 2022
  1. Using the -Like Logical Operator to Check the Beginning of a String Using PowerShell
  2. Using the -cLike Logical Operator to Check the Beginning of a String Using PowerShell
  3. Using the StartsWith() Function to Check the Beginning of a String Using PowerShell
Check the Beginning of a String Using PowerShell

There may be instances where we encounter a use case that may require functionality to check if a string variable starts with a character or a string.

Checking a string if it begins with a specific character or string is a common practice when making scripts and is also reasonably easy when written in Windows PowerShell.

This article will demonstrate how to check the beginning of a string variable using different methods in Windows PowerShell.

We can use the Windows PowerShell -like operator with a wildcard character to check the beginning of the string for both case-sensitive and case-insensitive. But before we discuss its syntax, we may need to clarify what a wildcard is.

Wildcards are systematic patterns that are used to match multiple characters. They specify specific patterns in the cmdlets to filter the results or return only the wildcard results. There are various types of wild cards that are available in Windows PowerShell. They are represented as *, ?, [m-n], and [abc].

We will use the asterisk (*) wildcard in this specific use case. The asterisk wildcard denotes that the matching pattern must contain zero or more characters. So, for example, the string ba* may correspond to bat, bath, bars, basil, basilica, or simply ba.

Using the -Like Logical Operator to Check the Beginning of a String Using PowerShell

The following method checks if a string starts with another string using the -like operator. By default, the -Like operator ignores the case-sensitive statement. However, as mentioned before, if we use logical operators, it must be partnered with an asterisk wildcard.

Example Code:

$strVal = 'Hello World'
if($strVal -like 'hello*') {
      Write-Host "Your string starts with hello."
} else {
      Write-Host "Your string doesn't start with hello."
}

Output:

Your string starts with hello.

Using the -cLike Logical Operator to Check the Beginning of a String Using PowerShell

We may use the -cLike operator to perform a case-sensitive comparison.

$strVal = 'Hello World!'
if($strVal -clike 'h*') {
      Write-Host "Your string starts with lowercase h."
} else {
      Write-Host "Your string starts with uppercase H."
}

Output:

Your string starts with uppercase H.

Using the StartsWith() Function to Check the Beginning of a String Using PowerShell

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

The following method checks if a string starts with another string.

$strVal ='Hello World!'
if($strVal.StartsWith('Hello')) {
     Write-Host 'Your string starts with hello.'
} else {
     Write-Host 'Your string doesn't start with hello.'
}

The StartsWith function also accepts another argument that we can use to check for case-sensitive characters. This argument is CurrentCultureIgnoreCase. Use the following method if you want to perform a case-sensitive comparison.

$strVal ='Hello world'
if($strVal.StartsWith('hello','CurrentCultureIgnoreCase')) {
     Write-Host 'True'
} else {
     Write-Host 'False'
}
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell String