EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial String in PowerShell
 

String in PowerShell

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated June 28, 2023

String in PowerShell

 

 

Introduction to String in PowerShell

A string is considered a sequence of characters and is one of the datatypes in PowerShell. Its object type is System. String. When we mention string, everything in a double quote or single quote is considered a string; the only difference between them is a representation of a variable inside the string. In PowerShell, nearly every output can be converted into String objects.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How to Declare a String in PowerShell?

In PowerShell, anything you mentioned inside the double quote or single quote is considered a string; only the variable representation is different in both cases inside the string. There are a few methods with which you can declare a string. First, you can directly assign a string to a variable, and the variable’s data type becomes a string. For example, Here, $var is now a string variable. You can check its datatype.

Code:

$var = "This is a string"
$var.GetType()

Output:

String in PowerShell 1

The second method to declare a string is explicit. Here you are converting a variable to a string. The below method is generally used in function and advanced functions while declaring parameters.

Code:

[String]$var1 = "This is a string"

Operations Performed with String in PowerShell

To check which operations PowerShell can perform on a string, you must pipeline the Get-Member command and check for the methods.

Code:

$var | Get-Member

We will discuss here one by one operation.

1. To check the Length of the String

You can check the string length or the number of characters in the string you need to use the Length function of the string.

Code:

$var = "This is a test string"
$var.Length

Output:

String in PowerShell 2

2. Split Operation on a String

Applying a split operation on a string divides the string into multiple lines from where the split character is mentioned. Let’s take our environment PS module path example,

Code:

$env:PSModulePath

Output:

string in powershell 3

We want to split the path in the above output.

Code:

($env:PSModulePath).Split(';')

Output:

string in powershell 4

Similarly, you can divide the string into the desired output using different split characters.

3. Join operation on a String

When you use the Join operation on a string, it concatenates two strings.

Code:

"Hello" + " World"

Output:

join operation

Another method you can use is the Join command.

Code:

$a = "Hello"
$b = "World"
$a,$b -join(' ')

Output:

string in powershell 5

If you want to Join two strings with a ‘#‘ character, put the ‘#’ inside the Join function.

Code:

$a = "Hello"
$b = "World"
$a,$b -join('#')

Output:

string in powershell 6

You can join more than two strings as well.

Code:

$a = "Hello"
$b = "World"
$c = "Delta"
$a,$b,$c -join('#')

Output:

string in powershell 7

You can also join the string with the Concat method.

Code:

$a = "Hello"
$b = "World"
$c = "Delta"
[System.String]::Concat($a,$b,$c)

Output:

string in powershell 8

4. SubString Operation on a String

A substring command in a string is used to extract the part of the string. It works on indexing. In a string, the index starts from 0.

Code:

$var = "This is a string"
$var.Substring(0,5)

Output:

substring operation

To extract the entire string,

Code:

$var = "This is a string"
$var.Substring(0,$var.Length)

Output:

string in powershell 10

5. Contains Operation on a String

Contain function, when applied to a string, checks whether the specific word exists in the string. This function is case-sensitive. The output will be in a Boolean (TRUE / FALSE) format.

Code:

$var = "This is a string"
$var.Contains("this")
$var.Contains("This")

The output will be False and True, respectively. As this method is case sensitive, when a function matches “this,” it can’t recognize part in the string.

Output:

contains operation

You can also validate the part of the word in a string.

Code:

$var = "This is a string"
$var.Contains("ring")

The output will be True.

string in powershell 12

6. Upper Case and Lower Case Operation on a String

You can convert the entire string into the Upper or Lower cases.

Code:

$var = "This is a string"
$var.ToUpper()
$var.ToLower()

Output:

upper case lower case operation

7. Trim Operation on a String

Trim operation in PowerShell is used to trim or remove the space or specific characters from the string’s beginning and end. For example,

Using only the Trim() function on the string will remove the space from the beginning and the end of the string. This operation is case-sensitive when you use any character inside the function.

Code:

(" Hello World ").Trim()

Output:

trim opeartion

To trim the specific characters from both ends,

Code:

("Hello World").Trim('H') #Removes the 'H' from the beginning
("Hello World").Trim('Hd') #Removes the 'H' from the beginning and 'd' from the end.
("Hello World").Trim('e') #Removes nothing as 'e' is not beginning or the end of the string

Output:

trim opeartion

TrimStart() and TrimEnd() functions also remove the space or the specific character from the beginning and the end of the string, respectively.

TrimStart() example,

Code:

("Hi, this ends with H").TrimStart('H')

Output:

string in powershell 16

TrimEnd() example,

Code:

("Sir, this ends with S").TrimEnd('S')

Output:

string in powershell 17

When you mention multiple characters together, the operation removes them.

Code:

("Hello World").TrimStart("Hel")
("Hello World").TrimEnd("ld")

Output:

string in powershell 18

8. Replace operation on a String

A Replace operation is available on a string command to replace the specific character or set of characters. This operation is not case-sensitive. The below command will replace ‘N’ with a null character.

Code:

("New PowerShell version is 7.0") -Replace ("N","")

Output:

Replace operation

You can replace the space with a specific character.

Code:

("New PowerShell version is 7.0") -Replace (' ','#')

Output:

replace operation

To replace the word as well,

Code:

("New PowerShell version is 7.0") -Replace ('PowerShell','Python')

Output:

string in powershell 21JPG

9. Index Operation on a String

Index operation in a PowerShell is used to find the position of the particular character in the string. Indexing starts at 0. IndexOf() operation finds the first occurrence of the character.

Code:

("New PowerShell version is 7.0").IndexOf('P')

Output:

index operation

LastIndexOf() finds the last occurrence of the character.

Code:

("New PowerShell version is 7.0").LastIndexOf('n')

Output:

string in powershell 23

10. Converting an Object into a String

To convert any object into a string data type, use ToString() command. In the below example, we will convert an integer value to a string.

Code:

$intvar = 12345
$intvar.GetType()

If you check the datatype of the $intvar, it will be the Integer type.

Output:

converting object into string

To convert the above variable into a string,

Code:

$intvar = 12345
$intvar = $intvar.ToString()
$intvar.GetType()

Output:

converting object into string

Conclusion

String operation is frequently used in PowerShell scripting as it is easy to perform an operation on the number of lines and extract the data. In PowerShell, most outputs are in String format or can be converted into a string. Another Pipeline cmdlet Select-String, effectively works on string operation when there is extensive data extraction through the Get-Content or XML.

Recommended Articles

This has been a guide to String in PowerShell. Here we discuss the introduction, operations, and how to declare a string in PowerShell. You may also have a look at the following articles to learn more –

  1. PowerShell Import Module
  2. PowerShell String Functions
  3. Guide to Python Import Module
  4. PowerShell Invoke-Command
  5. PowerShell Convert to String | Examples
  6. Guide to PowerShell String Replace

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW