如何在powershell中搜索字符串并删除该行?

时间:2015-07-21 23:35:32

标签: python shell powershell scripting powershell-v2.0

如果我在文本文件中有5个单词

red car

yellow flower

purple heart

vault of grass

hard light

如何搜索单词然后将其删除并保存/覆盖...在python中很容易,但对于我而言,powershell对我来说有点复杂。

到目前为止,这就是我所拥有的

#name new file
$fileName = read-host "Enter fileName: "

#create new text file to save processes
$textFile = New-Item  C:\Users\socrateslouis\Documents\$folderName\$fileName".txt" -type file

#store in text file
$outfile =Get-Process | Out-File -FilePath C:\Users\socrates\Documents\$folderName\$fileName".txt"
#display processes

$processFile = Get-Content C:\Users\socrates\Documents\$folderName\$fileName".txt"

$processFile

1 个答案:

答案 0 :(得分:1)

$File = 'TestFile.txt'
$tempFile = 'TestFileTmp.txt'
$stringToMatch = "vault"

Get-Content $File | Where { $_ -notmatch $stringToMatch } | Set-Content $tempFile
Remove-Item -Path $File
Rename-Item -Path $tempFile -NewName $File -Force