Convert-String

Formats a string to match examples.
Convert-String [-Example [<List`1>]] -InputObject* <String> [<CommonParameters>]

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

Parameters
-Example [<List`1>]

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 following example:

Patti Fuller = Fuller, P.

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

-InputObject <String>

  • This value is required
  • Accepts pipeline input ByValue

Specifies a string to format.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs

String

You can pipe strings to this cmdlet.

Outputs

String

This cmdlet returns a string.

Examples
  1. Convert format of a string:
    PS C:> $Names = "Evan Narvaez","David Chew","Elisa Daugherty"
    Convert-String -InputObject $Names -Example "Patti Fuller = Fuller, P."
    Narvaez, E. Chew, D. Daugherty, E.
    

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

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

  2. Format process information:
    PS C:> $Processes = Get-Process -Name "svchost" | Select-Object -Property processname, id | ConvertTo-Csv -NoTypeInformation
    PS C:>  $Processes | Convert-String -Example '"svchost", "219"=219, s.'
    716, s. 892, s. 908, s. 1004, s.
    ...
    

    The first command gets processes named svchost by using the Get-Process cmdlet. The command passes them to the Select-Object cmdlet, which selects the process name and process ID. The command converts the output to comma separated values without type information by using the ConvertTo-Csv cmdlet. The command stores the results in the $Processes variable. $Processes now contains SVCHOST and PID.

    The second command specifies an example that changes the order of the items and abbreviates svchost. The command coverts each string in $Processes.

Related Links

ConvertFrom-String
ConvertTo-Csv
Get-Process
Out-String
Select-Object