EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Convert to String

PowerShell Convert to String

By Priya PedamkarPriya Pedamkar

PowerShell Convert to String

Overview of PowerShell Convert to String

One of the most commonly used string manipulations is the swapping of first and last name of the user. i.e, the last name needs to come first followed by the first name. To achieve these kinds of functionalities the Convert-String cmdlet in PowerShell is used. The convert-string cmdlet was first introduced in the PowerShell Version 5.0. It is also one of the most underrated and unexplored cmdlets by the users. The use of the convert-string cmdlet is that it can format the string based on the example given by the user. The cmdlet takes a sample input from the user, then formats the output in the same pattern of the input.

Syntax

Below is the syntax :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

NAME
Convert-String
SYNTAX
Convert-String -InputObject <string> [-Example <List[psobject]>] [<CommonParameters>] ALIASES
None

Explanation of the above syntax:

This denotes the sample format in which the output must be produced. The source pattern is specified on the left side of the equal sign (=) and the right side denotes the target format. Alternatively, a hash table type before and after properties can be used to format the string. It must be carefully observed not to leave a space after the equal sign. Its type is a generic list of PowerShell objects. Its alias is E. Default is none. It doesn’t accept pipeline input and wildcard characters are not allowed.

Parameter of convert to string

Below is the different parameter of convert to string:

1. -InputObject

This denotes the input string to be formatted. Its type is a string. The default value is none. It accepts pipeline input, but wildcard characters are not allowed.

Example #1

Code:

Write-Host "Welcome to convert string tutorial"
[email protected]("vignesh krishnakumar", "Nandhini venkatesan", "Vyapini Vignesh", "Rafa Nadal", "input output", "Krishnakumar sankaran", "Durgalakshmi Krishnakumar")
Write-Host "The input string is as follows" $input
Write-Host "Interchanging first and last names"
write-host "Interchnaged name is"
$input | Convert-String -Example "one two= two,one"

Output:

powershell convert to string output 1

Example #2

Input:

Write-Host "Welcome to convert string tutorial"
[email protected]("vignesh one krishnakumar", "Nandhini two venkatesan", "Vyapini three Vignesh", "Rafa four Nadal", "input five output", "Krishnakumar six sankaran", "Durgalakshmi seven Krishnakumar")
Write-Host "The input string is as follows" $input -ForegroundColor Yellow
Write-Host "Removing last name and appending hypen"
write-host "Interchnaged name is"
$output=$input | Convert-String -Example "one three two=three-one"
Write-Host $output -ForegroundColor Yellow
Write-Host "Example when strings doesnt match the format"
[email protected]("vignesh krishnakumar", "Nandhini two venkatesan", "Vyapini Vignesh", "Rafa four Nadal", "input five output", "Krishnakumar sankaran", "Durgalakshmi seven Krishnakumar")
$output=$input | Convert-String -Example "one three two=three-one"
Write-Host $output -ForegroundColor Yellow

Output:

input object

Example #3

Input:

Write-Host "Welcome to convert string tutorial"
Write-Host "Applying multiple patterns to the input"
$SamplePatterns = @(
@{before='"one","two"'; after='two: one'},
@{before='"world","1"'; after='1: world'},
@{before='"one-two","22"'; after='22: one-two'},
@{before='"hi hello","333"'; after='333: hi hello'}
)
$input = Get-Process  | Select-Object -Property ProcessName, CPU | ConvertTo-Csv -NoTypeInformation
$input | Convert-String -Example $SamplePatterns

Output:

input object

2. Out-String

This cmdlet is used to convert the PowerShell object into strings.

Syntax:

NAME
Out-String
SYNTAX
Out-String [-Stream] [-Width <int>] [-InputObject <psobject>] [<CommonParameters>] ALIASES
None

Parameters of Out-String

Below is the different parameters of out-string:

1. -InputObject

It denotes the objects to be converted as a string. It can be a variable that holds objects, an expression or a cmdlet that will return objects. Its type is PS Object. The default value is none. It accepts pipeline characters, but wildcard characters are not accepted.

2. -NoNewline

It denotes newlines that are not to be added, if the string objects contain newlines then those are not removed. Its type is a switch parameter. The default value is none. It doesn’t accept pipeline characters, also wildcard characters are not accepted.

3. -Stream

It denotes for each object a separate string is sent in the output. Its type is a switch parameter. The default value is false. It doesn’t accept pipeline characters, also wildcard characters are not accepted.

4. -Width

It denotes the number of characters that should be present in the output, additional characters are truncated. The default value of the characters is 80. The default value is none. It doesn’t accept pipeline characters, also wildcard characters are not accepted.

Example #1

Input:

Write-Host "Welcome to out string example"
Write-Host "Reading a file and outing as string"
Write-Host "Reading the content...."
$input=get-content "C:\Vignesh\KB.txt"
Write-Host "Converting to string"
$input | Out-String

Output:

powershell convert to string 4

Example #2

Input:

Write-Host "Welcome to out string example"
Write-Host "find all the alias with get"
get-alias | out-string -stream | select-string "Get-"
Write-Host "find all the alias with set"
get-alias | out-string -stream | select-string "Set-"
Write-Host "find all the alias with write"
get-alias | out-string -stream | select-string "Write-"
Write-Host "find all the alias with Out"
get-alias | out-string -stream | select-string "Out-"

Output:

powershell convert to string 5

Example #3

Input:

Write-Host "welcome to conversion of int to string"
$int= 12
Write-Host "type before conversion is" $int.GetType()
Write-Host "Converting to int"
Write-Host $int
$new=$int.ToString()
Write-Host "Now the type is" $new.GetType() $new
Write-Host "another way of converting int to string"
$number=1654
Write-Host "The data type is" $number.GetType()
Write-Host "Converting to string"
$str=[String]$number
Write-Host "Converted data type is" $str.GetType()

Output:

powershell convert to string 7

Example #4

Input:

Write-Host "coversion of date time to string"
$date= Get-Date
Write-Host $date
Write-Host "The type is "$date.GetType()
Write-Host "First way of converting to string"
$first = '{0:MM/dd/yy}' -f $date
Write-Host "The type now is" $first.GetType()
Write-Host "Second way of converting to string"
$second = $date.ToShortDateString()
Write-Host "Now the type is" $second.GetType()
Write-Host "Third way of converting to string"
$Third = [String]$date
Write-Host "Now the type is" $Third.GetType()

Output:

host conversion

Conclusion

Thus, the article is covered in detail about Convert-String cmdlet. It also showed various examples of how to use the cmdlet and the parameters that are associated with it. The article, also covered in detail about the Out-String cmdlet, the associated parameters along with appropriate examples. The article also covered how an integer or a date-time object can be converted to a string variable using the appropriate methods and with the help of typecasting. To cover more and learn in detail, it is advisable to write sample scripts and practice them.

Recommended Articles

This has been a guide to PowerShell Convert to String. Here we discuss the basic concept with parameters and examples of PowerShell Convert to String in detail. You may also have a look at the following articles to learn more –

  1. Reverse String in Java
  2. String in PowerShell
  3. PowerShell Date
  4. PowerShell ForEach Object
MICROSOFT POWER BI Training
48+ Hours of HD Videos
8 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
CYBER SECURITY & ETHICAL HACKING Certification Course
89+ Hours of HD Videos
13 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE
97+ Hours of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
KALI LINUX Certification Course
26+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
WINDOWS POWERSHELL Certification Course
 26+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
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

© 2023 - 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

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
Let’s Get Started

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
EDUCBA

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

Forgot Password?

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