How to Extract a Powershell Substring from a String

-

|

Introduction

A substring is a part of a string. You can create a PowerShell substring from a string using either the substring, split methods. An example of a string is subdomain.domain.com. The substrings of subdomain.domain.com are subdomain, domain, or com.

In this guide, you will learn different ways to get a PowerShell substring from a PowerShell string.

Advertisement

Extract a PowerShell Substring With Substring or Split Methods

A PowerShell method specifies actions you can perform in an object. This section shows how to use the Substring or Split Methods to get a PowerShell Substring from a string.

Substring and Split methods are part of the System.String class. To get all the methods in an object use the Get-Member cmdlet.

Before we dive in, let’s look at the syntax of these methods.

The syntax of the substring method:

 string.substring(int startIndex)
string.substring(int startIndex, int length)

startIndex is the starting character (zero-based) position of a substring.
length is the number of characters in the substring

The syntax of the split method is shown below:

string[] Split(String[] separator, StringSplitOptions options)

PowerShell Substring Example (Substring Method)

As stated earlier, the Substring Method has the following syntax:

string.substring(int startIndex)

or

string.substring(int startIndex, int length)

In this example, I will show how to extract the subdomain string from a fully qualified domain name.

Let’s say 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.

Let’s assume that you are writing a PowerShell script that requires you to run conditional commands based on the country subdomain name. You can use PowerShell substring, substring method to get the subdomain for each country.

There are two ways you can achieve this:

Method 1: Accessing the string directly.

Open a PowerShell command prompt and enter the following

("USA.iTechGuides.com").Substring 

Then press the tab key. PowerShell will automatically enter an opening bracket, ( as shown below:

("USA.iTechGuides.com").Substring(

Next, enter the startIndex followed by a comma (,). Before you do this, remember we want to extract “USA”. In this instance, our startIndex is zero (0). We want to extract from the first character. The command will now look like this:

("USA.iTechGuides.com").Substring(0,

Next, enter the length and then a closing bracket, ). The length is the number of characters to extract, starting from the startIndex. We want to extract 3 characters, “USA”. Our length is 3.

Here is the final command:

("USA.iTechGuides.com").Substring(0, 3)

Here is the result in Powershell.

Powershell Substring example - Substring  method

What if we want to extract the parent domain, iTechGuides.com? What will be our startIndex and length? To determine your startIndex, starting from zero (0), count from “U” and end in dot before “iTechGuides”, the startIndex is 4. Then we want to count characters in “iTechGuides.com”. This makes our length 15.

Our command is:

("USA.iTechGuides.com").Substring(4, 15)

This returned iTechGuides.com!

Method 2: Accessing the string via a variable.

Another way you can get the PowerShell substring is to add the original string into a variable.

Here is how:

Create a variable with “USA.iTechGuides.com”. I will call the variable $domain

$domain = "USA.iTechGuides.com" 

Once you have done that, you can use the same substring method as shown in the first method. Here are the commands and the results in PowerShell.

Advertisement

PowerShell Substring Example (Split Method)

We can achieve the same result using the split method instead. In this example, we will also extract “USA” and “iTechGuides.com” from USA.iTechGuides.com.

Just like we did in the Substring method example, we can access the string directly or via a variable. In this example, I will use the variable.

Here are the steps:

  • Create a variable as shown below
 $domain = "USA.iTechGuides.com"  
  • Then split the string using the dot (.) as the separator
$domain.Split(".")
PowerShell Substring Example (Split Method) - example 1
  • To extract “USA”, use the command below
$domain.Split(".")[0]

Adding [0] to the end of the command, extracts the first string.

I am sure you are thinking “how do we extract “iTechGuides.com” “? It is fairly straightforward. First, extract the second substring, iTechGuides. Then the third, com. Finally, use the join operator to join both with a dot (.) in between.

Here are the commands

  • Extract the second substring, iTechGuides
$domain.Split(".")[1]
  • Then, the third substring com.
$domain.Split(".")[2]
  • Then join both with a dot (.) between the two substrings using the join operator. Here is the command:
-join ($domain.Split(".")[1],".",$domain.Split(".")[2])

Here is the final result in PowerShell:

Conclusion

There are so many reasons you may want to extract a PowerShell substring from a string. I hope this guide made life easier for you!

Have questions? Use the “Leave a Reply” form found at the end of this page. You could also share your experience working with substrings in PowerShell.

Other Helpful Guides

Additional Resources and References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

Advertisement

BEST OF ITECHGUIDES

Absolute Reference vs Relative Reference Excel

Absolute Reference vs Relative Reference Excel: Quick Guide

What is Absolute Reference in Excel? Absolute reference in excel allows you to reference a particular row or column...
Dynamic disks bs basic disks

Dynamic Disk vs Basic Disk: How to Convert to Dynamic Disk

Introduction Dynamic disk type was introduced by Microsoft with Windows 2000. Earlier versions of Windows Operating Systems used Basic...
windows update 0x8024401c

How to Fix Windows Update Error 0x8024401c in Windows 10

Introduction Received Windows Update 0x8024401c error in Windows 10? You have done this and done that yet, the error...
hard drive not detected

How to Fix Hard Drive not Detected Problem

Introduction Some Windows users have experienced a situation where a hard drive is not detected after upgrading to Windows...
How to Change Outlook Password in 3 Different Ways

How to Change Outlook Password in 2 Easy Steps

Introduction Before you change your Outlook Password you have to first change it with your email provider. The reason...

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

excel document not saved

How to Fix “Document not Saved” Error in Excel

Excel "Document not Saved" Error may be caused by one of the following: Attempting to save a shared...

How to Fix “Your PC Ran into a Problem and Needs to Restart” in...

Introduction "Your PC Ran into a Problem and Needs to Restart" in Windows Server 2016 error may be caused...
windows safe mode

2 Easy Ways to Start Windows 10 Safe Mode

Introduction In previous versions of Windows you could start safe mode by pressing F8. But in Windows 10, F8...
Walmart Baby Registry

Walmart Baby Registry: Your Definitive Guide

Introduction Walmart Baby Registry gives expectant mothers a place to create a list of items they need for their...
disable cortana windows 10 featured

How to Disable Cortana in Windows 10 (2 Methods)

Introduction Some Windows 10 users may not like Cortana. Solution? Disable Cortana. You are probably reading this because you...

By using this website you agree to accept our Privacy Policy and Terms & Conditions