Powershell Substring Extract A Powershell Substring From A String


An example of a string is subdomain.domain.com. The substrings of subdomain.domain.com are subdomain, domain, and com. In this Itechguide, I will teach you all you need to learn about PowerShell Substring. Specifically, you will learn how to extract substrings from strings using the Substring and Split Methods. Additionally, you will learn about the PowerShell Split Operator. Finally, you will learn how to break a PowerShell string into substrings and use the Substring Method to extract the substrings.

How To Extract A PowerShell Substring With Substring Method

In this first section, you will learn how to extract substrings from a string with the Substring Method. I will kick of this section by explaining the syntax of the Substring Method.

Syntax of The PowerShell Substring Method

The syntax of the Substring Method: To ensure that you understand the remaining parts of this section, it is extremely important that you pay attention to my explanation of these syntaxes. As you can see from the syntax, when you specify the Substring Method of a string, you enter startIndex AND length. The startIndex is the first character of the substring you want to extract (counting from left to right and starting from adding 1 to the result of the IndexOf – except for the first character). Furthermore, the length is the number of characters you want to extract, starting from 1 and counting from the first character of the substring you want to extract. To help you understand this concept, the next section will discuss IndexOf and LastIndexOf Methods.

How To Identify A PowerShell Substring Position With IndexOf And LastIndexOf Methods

When I explained the syntax of the Substring Method, I said that the startIndex of the PowerShell Substring Method is the first character of the substring you want to extract (determined by adding 1 to the result of the IndexOf value for that substring). In this section, I will use the IndexOf and LastIndexOf Methods of a PowerShell string to show you how PowerShell determines the positions of substring characters in a string. To illustrate, let’s examine the index position of the first letter “u” in usa.itechguides.com – by running the command below… The result is zero (0)… Why is this important? Glad you asked! You remember the startIndex AND length in the syntax of the PowerShell Substring Method? If the index position of the letter “u” in usa.itechguides.com is zero (o), according to the IndexOf Method – it means that if we wish to extract characters starting from the letter “u”, our startIndex will be 0 – since it is the first character in the string. How amazing! Now, lets add another layer to this. What if we want to find the last occurrence of a character in a string? Instead of using, the IndexOf Method, we will use the LastIndexOf Method. This command will determine the location of the last occurrence of the letter “u” in the string, usa.itechguides.com: The result is 10…

How To Extract A PowerShell Substring Left Of A String

In this section, you will learn how to extract a substring from the left of a string. I will show you an example that can be used in production environments. Assuming you manage an Active Directory domain with multiple subdomains. The subdomains are named using a 3 letter country name. Examples of your subdomains are USA.itechguides.com, JPN.itechguides.com. Furthermore, let’s assume that you are writing a PowerShell script that requires you to run conditional commands based on the country subdomain name – within you Active Directory domain. In this section, I will teach you how to extract the subdomain USA, from USA.itechguides.com. Let’s start by saving the subdomain, USA.itechguides.com in a variable called subdomain. Follow the steps below to extract USA, from USA.itechguides.com: To see the command in action, run the command below, first… This saves the string, USA.itechguides.com in the PowerShell variable, $subdomain. Then, run the command that extracts the “USA” substring from the string, USA.itechguides.com. As expected, the result is “USA”! What if you want to extract the parent domain, itechguides.com from USA.itechguides.com? What will be our startIndex and length? To determine your startIndex, first run the IndexOf command, using the first period (.) as your input. The result is 3… According to my previous explanation, to determine the startIndex from the result of the IndexOf command, add 1. If we add 1 to 3, our startIndex is 4. Next, to determine the length, start counting from 1, beginning from the first character of the string you want to extract. Then, stop at the last character you want to extract from the string. Remember that we want to extract itechguides.com from USA.itechguides.com? Counting from the first character of the string we want to extract (i), beginning at 1, the length is 15 (itechguides.com is 15 characters). Based on these values, the command that extracts the PowerShell substring, itechguides.com from USA.itechguides.com is… Here is the result, itechguides.com! Finally, to help simplify this fairly confusing concept of startIndex, IndexOf, and length, here is a summary:

How To Extract A PowerShell Substring Before And After A Specified Character

In this section, I will teach you how to extract PowerShell substring before and after a specified character. To illustrate this, let’s go back to our subdomain… Say I want to extract the substring before ad after the first period (.). Effectively, I want to extract “USA” (the substring before the specified character). Additionally, I want to extract “itechguides.com” (the substring after the specified character). The first step is to use the IndexOf Method to determine the position of the reference character. The command below saves the position of the reference character in a variable called refcharacter. Next, use the substring Method to extract the substring before the reference character. Here is the result… Finally, use the following PowerShell substring Method to extract the substring after the reference character As expected, the result extracts the substring after the period, which is itechguides.com!

How To Extract A PowerShell Substring Between 2 Characters

In this section, I will teach you how to extract “itechguides” from “USA.itechguides.com“. In this example, I want to extract a PowerShell substring between 2 characters. The two characters in this example are the first and last periods (.). The script requires 3 commands. In the first command, we use the IndexOf Method to determine the position of the first reference character (.). Then, save the result in a variable called firstref. Next, we use the LastIndexOf Method to determine the location of the last reference character. The result is saved in lastref variable. Finally, to extract the PowerShell substring, itechguides between 2 characters (the periods), run the command below… In the command, we added 1 to the result of the IndexOf command. In this example, 3, giving a total of 4. This makes our startIndex 4. Additionally, in the PowerShell substring Method, we used the length as $lastref-4. From our previous commands, $lastref is the variable we saved the result of the LastIndexOf command. The result is 15. Removing 4 from 15 makes our length 11. The result from the last command is itechguides!

How To Extract A PowerShell Substring With Split Method

In the first section of this guide, I discussed how to extract a PowerShell substring with the Substring Method. In this section, you will learn how to extract PowerShell substring with the Split Method. As usual, I will start this section with the syntax of the PowerShell Split Method.

Syntax of The PowerShell Split Method

The syntax of the Split Method for extracting a PowerShell substring is: The delimiter is any character you want to use to split the PowerShell string into individual substrings. The “substring position” placed in the [] bracket is used to select a specific substring you want to return from the strings.

How To Use Multiple Delimiters With PowerShell Split

As seen in the syntax of the PowerShell Split Method, it allows only one delimiter. However, if you want to split substrings from a string with multiple delimiters, you have to use the PowerShell Split Operator, instead. The syntax of the PowerShell Split Operator is… If you want to use multiple delimiters, first save the string in a variable – in this example, $string. Then, use the automatic pipeline variable $_ to add the multiple delimiters as shown below: For example, we can split this Active Directory Distinguished Name, OU=LON,OU=DEV,DC=ITECHGUIDES,DC=COM – using the two delimiters, “=”, and “,”. First, let’s save the DN in a variable called $ADDN… Then, to split the string using the “=”, and “,” delimiters, use the command below: Bingo, the last command split the string into individual PowerShell substrings! In the next section, you will learn how to return one of the substings.

How To Extract A PowerShell Substring With The Split Method

In the last section, you saw how to split a string into individual substrings using the PowerShell Split Operator. Here is the last command… The command split the string into its individual substrings. When I discussed the syntax of the Split Method for extracting a PowerShell substring is, I included a [] bracket that contains the position of the substring you want to return: PowerShell numbers the positions of substrings from zero (0). So, going back to the Active Directory DN example – see the screenshot below – the first substring “OU” is in position zero (0), “LON” is in position 1, and so on. So, to return “LON”, we will enclose the last command in the [1] bracket. In this instance, 1 is the position of the substring we want to return. Here is the command… Here is the result of the command: Alternatively, you could save the result of the command with the Split Operator into a variable. Then, return any of the substrings by enclosing the substring position in the [] bracket. For example, to return “LON”, we will use the command below: The result is the same as the previous command without the variable…

How To Extract The Last Element Of A Substring With PowerShell Split

If you want to return the first or last element (character) of a substring, you can use the same principle from our last example. While researching this topic, I stumbled on this stackoverflow.com thread. The user has a string shown below: The question is to return the first part of the string, 0.3.1 – according to this user, called tag. Additionally, the user wants to return the last part of the string, g3b885c5, called the CommitId. The first step is to save the string into a variable (this is optional but generally makes life easier). Then, use the Split Method to split the string into PowerShell substrings using “-” as the delimiter. Once again, to make it easy for me to explain, I will save the result in another variable… To see the result of the last command, run the $TccSubstrings variable alone. Here it is… Based on our previous examples in this section, to extract the Tag (the first substring), use the command below: Finally, to extract the CommitId (the last substring), use the command below… And here are the results… Note that you can also solve this problem by running a single command… And here is the same result… There is no magic to this, all I did was combine all the commands into one command!

How To Split String And Remove Empty Entries With PowerShell Split

In this final example, I want to show you how to split a string with empty spaces (or entries) into substrings. Then, remove the empty spaces in the final result. In another stackoverflow.com thread, a user posted this string saved in a variable: The user then used the Split Method to split the string into individual substrings. According to the user (and this is obvious), the problem is that result has spaces. The person that posted this question wanted the spaces removed from the substrings. There are multiple solutions but one quick way is to pipe the output of the split command to Where-Object. This is courtesy of Rynant. Here is the command… And here is the result… That is it, our updated PowerShell substring article! I hope you found it helpful. If you found this article helpful, would you mind sparing 2 minutes to share your experience with our community members at Itechguides Community Forum. However, if you have any questions regarding this article or if the steps did not fix your problem, please post your question at Itechguides Community Forum. Our team and other community members will come back to you with a fix as soon as possible. Finally, for more PowerShell tech Itechguides, visit our Windows PowerShell How-To guide page. You may also find our Work from Home page very helpful.

References and Further Reading