Table of Contents
Using StartsWith()
Method
Use the StartsWith()
to check if string starts with another string in PowerShell.
1 2 3 4 5 6 7 8 |
$text = "Hello, world!" $prefix = "Hello" $startsWithPrefix = $text.StartsWith($prefix) Write-Host "Text: $text" Write-Host "Prefix: $prefix" Write-Host "Does '$text' start with '$prefix'? $startsWithPrefix" |
1 2 3 4 5 |
Text: Hello, world! Prefix: Hello Does 'Hello, world!' start with 'Hello'? True |
In the above code, a string variable named $text
is initialized with the value of Hello, world!
and another string variable called $prefix
is created with the value of Hello
. After that, using the StartsWith()
method, the function checks if $text
starts with the $prefix
, assigning the result (either true or false) to $startsWithPrefix
. Finally, it displays the result using the Write-Host
cmdlet.
If you want to ignore case, pass another parameter
CurrentCultureIgnoreCase
toStartsWith()
method.
12345678 $text = "Hello, world!"$prefix = "hello"$startsWithPrefix = $text.StartsWith($prefix,'CurrentCultureIgnoreCase')Write-Host "Text: $text"Write-Host "Prefix: $prefix"Write-Host "Does '$text' start with '$prefix'? $startsWithPrefix"
1 2 3 4 5 |
Text: Hello, world! Prefix: hello Does 'Hello, world!' start with 'hello'? True |
Using -like
Operator
Use the -like
operator to determine if string starts with another string in PowerShell.
1 2 3 4 5 6 |
$string = "Hello World" if ($string -like "H*") { Write-Host "String starts with H" } |
1 2 3 |
String starts with H |
The -like
operator compares a string with a pattern and returns a Boolean value of $true
if the string matches the pattern; otherwise, $false
.
The -like
operator in the above code determines whether the $string
starts with the letter H
(remember that the *
is a wildcard that matches zero or more random characters). The code utilizes the Write-Host
cmdlet to output the message String starts with H
if $string
begins with H
. The code does nothing if the $string
does not start with the letter H
.
Assume you have a situation where you are only required to check if the specified string starts with an alphabet, number or neither. In that case, you can use the -match
operator as follows.
Using -match
Operator
Use the -match
operator to check if the specified string starts with an alphabet, digit or neither of them in PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 |
$string = "123Hello World" if ($string -match "^\d"){ Write-Host "String starts with a digit" } elseif ($string -match "^[A-Za-z]"){ Write-Host "String starts with an alphabet" } else{ Write-Host "String neither starts with a number nor an alphabet" } |
1 2 3 |
String starts with a digit |
The above code snippet creates a string variable like the previous two code fences; it then uses the -match
operator with regular expression patterns to assess if the given string begins with a digit or an alphabet. If both cases are not satisfied, then it executes the else
block saying the specified string neither starts with a digit nor an alphabet.
However, the ^\d
and ^[A-Za-z]
expressions were used to check if the $string
starts with a digit or an alphabet at the beginning of the string. In the ^\d
expression, the character d
is used to match any digit (the same as [0-9]
), while the symbol ^
denotes the beginning of the string.
Similarly, in the ^[A-Za-z]
expression, [A-Za-z]
matches any upper/lower case alphabet, while ^
represents the start of the string. Finally, we used the Write-Host
cmdlet to print a customized message based on whether $string
start with a digit, an alphabet, or neither.
Using IndexOf()
Method
Use the IndexOf()
method to determine if string starts with another string in PowerShell.
1 2 3 4 5 6 |
$string = "Hello World" if ($string.IndexOf("H") -eq 0){ Write-Host "String starts with H" } |
1 2 3 |
String starts with H |
This function gives us the position of the first instance of a specified character or string in a given string. For example, if the returned index is 0
, it indicates that the string begins with the specified value.
The above code snippet uses the IndexOf()
method to find the index of the first occurrence of the character H
in the $string
. So, for example, if the string’s first character stored in the variable $string
is H
(i.e., if its index is 0
), the code will utilize the Write-Host
cmdlet to display the message on the console.
Further reading:
Using Substring()
Method
Use the Substring()
method to check if string starts with another string in PowerShell.
1 2 3 4 5 6 |
$string = "Hello World" if ($string.Substring(0,1) -eq "H") { Write-Host "String starts with H" } |
1 2 3 |
String starts with H |
The above code uses the Substring()
method to extract the first character of the $string
, starting at index 0
and including the 1
character. If the extracted character is equal to the "H"
, the code uses the Write-Host
cmdlet to print out the message String starts with H
.
Considering the above solutions, checking if a string starts with a specific set of characters in PowerShell is a simple task that can be accomplished using the Startwith()
method, -like
operator, match
operator, IndexOf()
, and Substring()
method. Furthermore, understanding these tools allows you to easily manipulate and extract substrings within larger strings to perform various tasks.
That’s all about PowerShell check if String starts with.