0
我想从多个文本文件中删除具有定义内容的行。从PowerShell中的多个文本文件中删除行
它在核心中工作,但即使没有进行任何更改,它也会重写每个文件,如果您只是从大约3000个logonscripts中修改50个文件,这并不酷。
我什至做了一个if语句,但它似乎不起作用。
好吧,这是我已经有:
#Here $varFind will be escaped from potential RegEx triggers.
$varFindEscaped = [regex]::Escape($varFind)
#Here the deletion happens.
foreach ($file in Get-ChildItem $varPath*$varEnding) {
$contentBefore = Get-Content $file
$contentAfter = Get-Content $file | Where-Object {$_ -notmatch $varFindEscaped}
if ($contentBefore -ne $contentAfter) {Set-Content $file $contentAfter}
}
什么意思变量:
$ varPath是其中logonscripts是路径。
$ varEnding是要修改的文件的文件结尾。
$ varFind是触发删除行的字符串。
任何帮助将不胜感激。
问候
LOWA美分
嗨马特你的代码就像一个魅力!非常感谢你! –