Convert-String (Microsoft.PowerShell.Utility) - PowerShell (2024)

  • Reference
Module:
Microsoft.PowerShell.Utility

Formats a string to match examples.

Syntax

Convert-String [-Example <System.Collections.Generic.List`1[System.Management.Automation.PSObject]>] -InputObject <String> [<CommonParameters>]

Description

The cmdlet formats a string to match the format of examples.

Examples

Example 1: Convert format of a string

"Mu Han", "Jim Hance", "David Ahs", "Kim Akers" | Convert-String -Example "Ed Wilson=Wilson, E."Han, M.Hance, J.Ahs, D.Akers, K.

The first command creates an array that contains first and last names.

The second command formats the names according to the example. It puts the surname first in theoutput, followed by an initial.

Example 2: Simplify format of a string

$composers = @("Johann Sebastian Bach", "Wolfgang Amadeus Mozart", "Frederic Francois Chopin", "Johannes Brahms")$composers | Convert-String -Example "first middle last=last, first"Bach, JohannMozart, WolfgangChopin, FredericBrahms, Johannes

The first command creates an array that contains first, middle and last names. Note that the lastentry has no middle name.

The second command formats the names according to the example. It puts the last name first in theoutput, followed by the first name. All middle names removed; entry without middle name is handledcorrectly.

Example 3: Output management when strings don't match example

$composers = @("Johann Sebastian Bach", "Wolfgang Amadeus Mozart", "Frederic Francois Chopin", "Johannes Brahms")$composers | Convert-String -Example "first middle last=middle, first"Sebastian, JohannAmadeus, WolfgangFrancois, Frederic

The first command creates an array that contains first, middle and last names. Note that the lastentry has no middle name.

The second command formats the names according to the example. It puts the middle name first inthe output, followed by the first name. The last entry in $Composers is skipped, because itdoesn't match the sample pattern: it has no middle name.

Example 4: Caution with beauty spaces

$composers = @("Antonio Vivaldi", "Richard Wagner ", "Franz Schubert", "Johannes Brahms ")$composers | Convert-String -Example "Patti Fuller = Fuller, P."Wagner, R. Brahms, J.

The first command creates an array of first and last names. Note that second and fourth items havean extra trailing space, after the last name.

The second command converts all strings that match the sample pattern:word, space, word, and final trailing space, all of this before the equal sign(=). Also, notethe leading space in the output.

Example 5: Format process information with multiple patterns

$ExamplePatterns = @( @{before='"Hello","World"'; after='World: Hello'}, @{before='"Hello","1"'; after='1: Hello'}, @{before='"Hello-World","22"'; after='22: Hello-World'}, @{before='"hello world","333"'; after='333: hello world'})$Processes = Get-Process | Select-Object -Property ProcessName, Id | ConvertTo-Csv -NoTypeInformation$Processes | Convert-String -Example $ExamplePatternsId: ProcessName4368: AGSService8896: Amazon Music Helper4420: AppleMobileDeviceService...11140: git-bash0: Idle...56: Secure System...13028: WmiPrvSE2724: WUDFHost2980: WUDFHost3348: WUDFHost

$ExamplePatterns defines different expected patterns in the data, through examples.

The first pattern, @{before='"Hello","World"'; after='World: Hello'},reads as follows:

  • expect strings where a word comes enclosed in double quotes, then a comma,
  • and then the second, and last, word enclosed in quotes;
  • with no spaces in the string. On the output: place second word first,
  • without quotes, then a single space, and then the first word, without quotes.

The second pattern, @{before='"Hello","1"'; after='1: Hello'},reads as follows:

  • expect strings where a word comes enclosed in double quotes, then a comma,
  • and then a number enclosed in quotes;
  • with no spaces in the string. On the output: place the number first,
  • without quotes, then a single space, and then the word, without quotes.

The third pattern, @{before='"Hello-World","22"'; after='22: Hello-World'},reads as follows:

  • expect strings where two words with a hyphen in between come enclosed in
  • double quotes, then a comma, and then a number enclosed in quotes;
  • with no spaces between the comma and the third double quote.
  • On the output: place the number first, without quotes, then a single space,
  • and then the hyphenated words, without quotes.

The fourth, and final, pattern,@{before='"hello world","333"'; after='333: hello world'},reads as follows:

  • expect strings where two words with a space in between come enclosed in
  • double quotes, then a comma, and then a number enclosed in quotes;
  • with no spaces between the comma and the third double quote.
  • On the output: place the number first, without quotes, then a single space,
  • and then the words with the space in between, without quotes.

The first command gets all processes by using the Get-Process cmdlet. The command passes them to theSelect-Object cmdlet, which selects the process name and process ID. At the end of the pipeline, thecommand converts the output to comma separated values, without type information, by using theConvertTo-Csv cmdlet. The command stores the results in the $Processes variable. $Processesnow contains process names and PID.

The second command specifies an example variable that changes the order of the input items. Thecommand coverts each string in $Processes.

Note

The fourth pattern implicitly says that two or more words separated by spaces are matched. Withoutthe fourth pattern, only the first word of the string enclosed in double quotes is matched.

Parameters

-Example

Specifies a list of examples of the target format. Specify pairs separated by the equal sign (=),with the source pattern on the left and the target pattern on the right, as in the followingexamples:

  • -Example "Hello World=World, Hello"
  • -Example "Hello World=World: Hello",'"Hello","1"=1: Hello'

Note

The second example uses a list of patterns

Alternatively, specify a list of hash tables that contain Before andAfter properties.

  • -Example @{before='"Hello","World"'; after='World: Hello'}, @{before='"Hello","1"'; after='1: Hello'}

Caution

Avoid using spaces around the equal sign(=), as they are treated as part of the pattern.

Type:List<T>[PSObject]
Aliases:E
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InputObject

Specifies a string to format.

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

Inputs

String

You can pipe strings to this cmdlet.

Outputs

String

This cmdlet returns a string.

  • ConvertFrom-String
  • ConvertTo-Csv
  • Get-Process
  • Out-String
  • Select-Object
Convert-String (Microsoft.PowerShell.Utility) - PowerShell (2024)
Top Articles
Add A Coin/Token Into SafePal Wallet | SafePal Knowledge Base
What to Do When VA Appraisal is Too Low | VA Loans For Vets
English Bulldog Puppies For Sale Under 1000 In Florida
Katie Pavlich Bikini Photos
Gamevault Agent
Pieology Nutrition Calculator Mobile
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Compare the Samsung Galaxy S24 - 256GB - Cobalt Violet vs Apple iPhone 16 Pro - 128GB - Desert Titanium | AT&T
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Craigslist Dog Kennels For Sale
Things To Do In Atlanta Tomorrow Night
Non Sequitur
Crossword Nexus Solver
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Energy Healing Conference Utah
Geometry Review Quiz 5 Answer Key
Hobby Stores Near Me Now
Icivics The Electoral Process Answer Key
Allybearloves
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Marquette Gas Prices
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Vera Bradley Factory Outlet Sunbury Products
Pixel Combat Unblocked
Movies - EPIC Theatres
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Mia Malkova Bio, Net Worth, Age & More - Magzica
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Where Can I Cash A Huntington National Bank Check
Topos De Bolos Engraçados
Sand Castle Parents Guide
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hello – Cornerstone Chapel
Stoughton Commuter Rail Schedule
Nfsd Web Portal
Selly Medaline
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6193

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.