Thursday, May 25, 2023
HomeSoftware TestingPowerShell Substring Secrets and techniques Revealed: Develop into a Command-Line Wizard

PowerShell Substring Secrets and techniques Revealed: Develop into a Command-Line Wizard

The PowerShell substring () technique in PowerShell can be utilized to extract a portion of a string. The beginning and size of the substring we want to extract from a string could be specified utilizing this operate.

The PowerShell substring Substring technique will likely be described intimately on this article, together with a number of examples of easy methods to extract sure strings utilizing the startIndex and/or size features.

  1. PowerShell Substring Technique
  2. Discovering a PowerShell substring Utilizing the Size Property
  3. Working with Dynamic Strings
  4. Utilizing IndexOf in Substring

1. PowerShell Substring Technique

The Substring() technique in PowerShell can be utilized to find a string inside one other string. Each string object in PowerShell has this technique.

The syntax of the PowerShell substring Technique:

string.substring(int startIndex, int size)

As you possibly can see from the Syntax, you should enter each startIndex AND size when specifying a string’s substring technique. StartIndex and size are each numbers (integers), as indicated by the syntax’s use of the int image.

The primary character of the substring you wish to extract is the startIndex. counting from left to proper and starting at 1, aside from the primary character, including 1 to the IndexOf consequence.

Utilizing the well-known string “hiya world” as our place to begin will help illustrate this technique the most effective. Use the command under to solely extract the phrase “world” from the string:

"Howdy World".Substring(6)

Solely the substring’s place to begin, 6, has been equipped. On this method, the operate will extract all characters within the string starting at place 6.

"Hello World".Substring(6)

I’ve numbered the placement beneath every letter of the string to assist make clear the location of every character. As you possibly can see, the letter W is at place 6.

Howdy World

When solely the phrase Howdy is to be extracted from the string, we should provide a startIndex of 0 and a size of 5 characters:

"Howdy World".Substring(0,5)

2. Discovering a Substring Utilizing the Size Property

You would possibly have to find the PowerShell substring made up of the ultimate 4 characters. The group of characters from the fourth to the final place all the best way to the conclusion should be situated. Any size can be utilized for the string that you’re looking.

We will dynamically set the tip place through the use of the size of the string and subtracting a selected variety of characters from it, versus declaring the tip place as a constructive quantity counting from the left.

You would possibly wish to find the ultimate 4 characters utilizing the string $code = ‘KJBKBSKJ252125’. Quite than doing one thing related:


0 could be modified to $code.Size – 4 will return the final 4 characters even when the tip place isn’t used in any respect.

PowerShell Substring Secrets and techniques Revealed: Develop into a Command-Line Wizard 6
PowerShell Substring Secrets and techniques Revealed: Develop into a Command-Line Wizard 7

The PowerShell substring method will all the time default to the final character place if you happen to don’t specify the tip place. You may dynamically select substrings by subtracting from the size property of the string, which represents the overall variety of characters within the string.

3.  Working with Dynamic Strings

You could not all the time pay attention to the string’s size when working with dynamic strings. We will accomplish this by using the substring technique’s string size operate.

Let’s say we wish to extract the filenames with out the extension from a folder containing log recordsdata. The filename could be any size, nonetheless on this case the extension is 3 letters and a dot.

To be able to scale back the filename size to simply the filenames, we will do the next:

$filename = "la-ams-file02-log-1.log"
$filename.Substring(0, $filename.Size - 4)

This will likewise be utilized in reverse to extract solely the filename extensions. We provide a size of three characters for the filename, which we use to calculate the beginning place:

$filename = "la-ams-file02-log-1.log"
$filename.Substring($filename.Size - 3, 3)

On this occasion, we might merely return the remaining letters and don’t even have to specify the size of the substring to return (3):

$filename.Substring($filename.Size - 3)

4. Utilizing IndexOf in Substring

There are events while you want to extract a piece in the midst of a string when working with dynamical string lengths. Discovering or calculating the exact beginning place for the substring strategy is then the tough half.

Using IndexOf is one technique for doing this. Utilizing this system, chances are you’ll decide the place a personality or string is situated inside a string.

We’ll look within the string for the half that’s distinct earlier than the date-log. On this scenario, we might want to add 4 characters to it as a result of the IndexOf technique will return the start line of the string or character that you just appeared for:

$recordsdata = @(
$recordsdata | Foreach {
    # Get the place of the substring 'log-' in every filename and add 4 to this place.
    $half = $_.Substring($_.IndexOf('log-') + 4)
    # Returns 202223.log for instance

    # Take away the extension from the substring
    $date = $half.Substring(0, $half.Size - 4)
    write-host $date
    # Returns 202223 for instance

On this particular scenario, you might also use the strategy LastIndexOf() within the file names that include hyphens:

$recordsdata | Foreach {
    $half = $_.Substring($_.LastIndexOf('-') + 1)
    write-host $half
    $date = $half.Substring(0, $half.Size - 4)
    write-host $date



Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments