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
Die besten Trading Apps 2024 im Vergleich
Nmap Host Discovery: The Ultimate Guide
فیلم پیشنهاد بی شرمانه دوبله فارسی نماشا بدون سانسور
Doddridge County Cad Log
Legend Piece Trello
Weepinbell Gen 3 Learnset
Zack Fairhurst Snapchat
Lbl A-Z
Good Morning American Deals And Steals
Ynot Parking Visitors
Bengals Vs Bills Postponed Until When
Suspect may have staked out Trump's golf course for 12 hours before the apparent assassination attempt
Craigslist Va Charlottesville
Brownlow fashions are a national obsession. And we owe it in part to this stylist
The FOIA Process Demystified: How to Access Your DOE Q Security Clearance Records
Prestige Home Designs By American Furniture Galleries
Stephanie Palomares Obituary
Meg 2: The Trench Showtimes Near Phoenix Theatres Laurel Park
How to Find the Subdomains of a Domain | Geekflare
Churchill Downs Racing Entries
Keyc Weather Forecast
Busted Mugshots Columbia Ky
Results from Form 1 of Page crazybutkool/crear_post.htm
Trinidad And Tobago Passport Renewal In Usa
Lake County Florida Trash Collection Schedule
Best Bars Near Comerica Park
Ecampus Scps Login
Roanoke Skipthegames Com
Craigslist Pets Gainesville Fl
Doylestown (Pennsylvania) – Travel guide at Wikivoyage
Quincy Herald-Whig Obituaries Past 3 Days
Moparts Com Forum
Jesus Calling: How Well Are You Listening?
Bloomfield Nj Craigslist
Egizi Funeral Home Turnersville Nj
Www.cvs/Otchs/Sunshinehealth
90 Cent Store Near Me
4722 Hours In Days
Www.patientnotebook/Rpa
Different Types of Conditional Sentences in English: Definition and Examples | Proofreading
The Weather Channel - Radar
Kate Spade Outlet Altoona
They're Cast In Some Shows Crossword Clue
Shane Gillis Girlfriend: All About His Dating History, Career & More |Pudelek
Buick Env
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Remember the 'weatherstick'? Peoria's weather tower was visible for miles
The Handy Review - Must Read This Before Buying
Craigslist Pet Phoenix
Funbox Lone Tree Tickets
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.