EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial String in PowerShell
Secondary Sidebar
PowerShell Tutorial
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

String in PowerShell

By Chirag NagarekarChirag Nagarekar

String in PowerShell

Introduction to String in PowerShell

A string is considered as a sequence of characters and it 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 as a string, the only difference between them is a representation of variable inside the string. In PowerShell, nearly every output can be converted into String objects.

How to Declare a String in PowerShell?

In PowerShell, anything you mentioned inside the double quote or single quote is considered as a string only the variable representation is different in both the cases inside the string. There are few methods with which you can declare a string. First is 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()

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Output:

String in PowerShell 1

The second method to declare a string is an explicit method. Here you are converting variable to 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 that PowerShell can perform on a string, you need to 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

When you apply a split operation on a string it divides the string into multiple lines from where the split character is mentioned. Let take our environment PS module path example,

Code:

$env:PSModulePath

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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 use different split characters to divide the string into the desired output.

3. Join Operation on a String

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

Code:

"Hello" + " World"

Output:

join operation

Another method you can use the Join command.

Code:

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

Output:

string in powershell 5

If you want to Join two strings with ‘#’ character then simply 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

Substring command in a string is used to extract the part of the string. It totally 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 on a string, it checks if the specific word exists in the string or not. 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 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 case or the Lower case.

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 the specific characters from the beginning and the end of the string. For example,

When you use only Trim() function on the string then it 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

There are also TrimStart() and TrimEnd() functions, which 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

To replace the specific character or set of characters, there is a Replace operation available on a string command. 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 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, you need to 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. There is another Pipeline cmdlet Select-String which effectively works on string operation when there is a large extract of data 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. PowerShell Invoke-Command
  4. Guide to Python Import Module
  5. PowerShell Convert to String | Examples
  6. Guide to PowerShell String Replace
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more