从文本文件中删除特殊字符

时间:2013-10-28 12:19:19

标签: powershell-v3.0

我有一个文本文件,我想从该文件中删除特殊字符。例如:- 文本文件中的条目

++Mathematics
--Chemistry
(Physics)
$#History)
%%Geography

现在我想从文本文件中的字符串替换所有特殊字符,并希望输出为: - 数学 化学 物理 历史 地理

我在powershell脚本中使用了replace命令,但是尽管替换了特殊字符,它仍然删除了我的文本文件中的所有值。

get-content D:\Share\SCCM-Powershell_Commands\edit.txt
Foreach-object {$_ -replace "\&", "" -replace "\£", "" -replace "\+", "" -replace "\-", "" -replace "\)","" -replace "\(","" -replace "\$","" -replace "\#","" -replace "\*",""}|
Set-Content D:\Share\SCCM-Powershell_Commands\edit1.txt

get-content D:\Share\SCCM-Powershell_Commands\edit.txt

同时有人也可以建议如何开始学习powershell。我是powershell的新手,想学习Powershell脚本。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

foreach ($line in (Get-Content D:\Share\SCCM-Powershell_Commands\edit.txt)) {
    $line -replace "\&", "" -replace "\£", "" -replace "\+", "" -replace "\-", "" -replace "\)","" -replace "\(","" -replace "\$","" -replace "\#","" -replace "\*","" -replace "\%",""
} | Set-Content D:\Share\SCCM-Powershell_Commands\edit1.txt

Get-Content D:\Share\SCCM-Powershell_Commands\edit1.txt