[ad_1]
Just a general reusable solution:
function Replace-String {
[CmdletBinding()][OutputType([string])] param(
[Parameter(Mandatory = $True, ValueFromPipeLine = $True)]$InputObject,
[Parameter(Mandatory = $True, Position = 0)][Array]$Pair,
[Alias('CaseSensitive')][switch]$MatchCase
)
for ($i = 0; $i -lt $Pair.get_Count()) {
if ($Pair[$i] -is [Array]) {
$InputObject = $InputObject |Replace-String -MatchCase:$MatchCase $Pair[$i++]
}
else {
$Regex = $Pair[$i++]
$Substitute = if ($i -lt $Pair.get_Count() -and $Pair[$i] -isnot [Array]) { $Pair[$i++] }
if ($MatchCase) { $InputObject = $InputObject -cReplace $Regex, $Substitute }
else { $InputObject = $InputObject -iReplace $Regex, $Substitute }
}
}
$InputObject
}; Set-Alias Replace Replace-String
Usage:
$lookupTable |Replace 'something1', 'something1aa', 'something2', 'something2bb', 'something3', 'something3cc'
or:
$lookupTable |Replace ('something1', 'something1aa'), ('something2', 'something2bb'), ('something3', 'something3cc')
Example:
'hello world' |Replace ('h','H'), ' ', ('w','W')
HelloWorld
[ad_2]