Updated March 27, 2023
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 :
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"
$input=@("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:
Example #2
Input:
Write-Host "Welcome to convert string tutorial"
$input=@("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"
$input=@("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:
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:
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:
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:
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:
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:
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 –