Что-то, что было бы легче поддерживать, было бы так:
#Words to remove from string
$wordsToCut = "This","is"
#Phrase to remove words from
$phrase = "This is a test"
#Running through all words in words to remove
foreach ($word in $wordsToCut){
#Replace current word with nothing
$phrase = $phrase.Replace($word,"")
}
#Output end result
Write-Host $phrase
Вы также можете использовать трим, чтобы удалить любые начальные или конечные пробелы.Вышеприведенный код выводит:
тест