Powershell

Substrings in PowerShell

Strings denote the collection of characters in a sequence to represent the text. For example, if you take three letters of alphabets, four digits (0-9), and few symbols (@, #, $) and write all these characters jointly, it will form a string. The strings look like an array as the indexing rules are the same as in strings; their storage concept is similar to arrays; however, the difference lies in extracting a single data value stored in one memory chunk. You can find the data value in arrays by calling the array and passing the required index number. However, if you want to see any single character in a string or multiple characters, you can get help from the “substring” method of string. While using the substring method, you have to define the limit of characters you want to find.

To dig into substrings, first, we may go for strings in PowerShell.

How to define a string using PowerShell

You can define the string using single quotes and double quotes as well. However, both representations have some differences; For example, strings represented in single quotes consider all the values as constants, while the string written in double-quotes automatically fetches the values of variables.

Furthermore, the single quote and double quote differences are given below in the example. For instance, we have written in the string “Your PowerShell edition is: $PSEdition“; as the $PSEdition contains the value of the edition of your PowerShell.

> $pse=‘Your PowerShell edition is: $PSEdition

Once you execute this command, you can notice that the single quote string is printed as it is; now check the same using double quotes:

> $pse1= “Your PowerShell edition is: $PSEdition

Double quotes will print the edition of the PowerShell, which was stored in $PSEdition.

You can perform this operation using PowerShell ISE:

Write the same line inside the scripting pane of the PowerShell ISE and save the script:

'Your PowerShell edition is: $PSEdition'

We have saved the script as “ISE.ps1” in location “C:\scripts“:

To run the script, navigate your terminal to the location where the script is saved;

The output of the above script is given below:

Use the same code but change it with double quotes:

"Your PowerShell edition is $PSEdition"

Create a new script and save the code in its;

In our case, we have created “ISE2.ps1” script in the same directory “C:\scripts“:

The output of the script is shown below:

How to find any character using substrings

One way to find a string inside a string is by using the substring method. Everything is an object in the PowerShell. Moreover, everything has a method, and here substring method of String object will be used to find a string inside a string. So, for that, you have to open PowerShell ISE; after opening, create a string in the script pane and assign multiple characters to that string. We will explain the use of the substring method with an example:

For example, we have created a string in a PowerShell script and saved the script as “IS3.ps1“:

> $string = “this is power shell substring method”
> $string

When you run the script, it will show all the characters of the string:

To print selected characters of the string; for instance, to print only “this is PowerShell,” there are two things you have to consider:

  • int startIndex: This factor means that from which character do you want to start your substring value?
  • Int length: After deciding the start index, you have to choose the size of your substring; this means how many characters your substring will cover towards right?

As we are starting from the very first character of the string, so “int startIndex” will be (0) in our case: Moreover, let us say that we want to get “this is PowerShell,” so we would move 18 characters [including “space”] towards the right side: The code is given below:

> $string= "this is powershell substring method"
> $string.Substring(0,18)

We have created another script, “ISE4.ps1” Both the values are passed to the substring method as shown in the image below:

Run the script in the Output pane:

How to find substring before and after a specific character

If we have a string “this is windows, powershell ISE” and wants to see the substring before and after the character “,“; at first, we must identify the character, let us say the character is “,“:

To do this, we have to use the method “IndexOf“:

Step 1: At first, we will create a variable and store the index value of the string;

The demo code is given below,

> $string= "this is windows, powershell ISE"
> $ref=$string.Indexof(",")
> $ref

and we have stored this code in new script “ISE5.ps1“;

The output of script “ISE5.ps1” is shown below:

We have stored the index value of “,” in a variable “$ref” and will use this variable to find the substring:

Step 2: To find the substring before “,“;

You have to pass two parameters: one parameter is “0,” which shows that the result will start from the initial character of the string; moreover, the second parameter represents the index number of the character “,“.

As we have to use $ref variable from previous step: so we will make changes to the script “ISE5.ps1” and save the script as “ISE6.ps1”:

> $string= “this is windows, powershell ISE”
> $ref=$string.Indexof(,)
> $newstring=$string.substring(0,$ref)
> $newstring

The script is given below:

The output of this script is given below:

Step 3: And to find the substring after the character “,“, you have to pass only one parameter which will add “1” to the index number as shown below; the output will show the remaining characters of the string after “,“: the code is given below and we have saved the code in a new script “ISE7.ps1

> $string= "this is windows, powershell ISE"
> $ref=$string.Indexof(",")
> $ns=$string.substring($ref+1)
> $ns

The script “ISE7.ps1” is given below;

The script’s output is given below: and it is observed that characters after “,” are displayed.

Conclusion

Strings contain multiple characters and can be stored in a single variable. They play a crucial role while initializing variables in programming. A substring is the part of a string that contains few characters of a string.

In this guide, we have demonstrated the use of substrings in Windows PowerShell. One can find few characters using the methods given in this guide. Moreover, you can derive substring by targeting a specific character of a parent string.

About the author

Adnan Shabbir