Appearance
文件
PowerShell 运行文件时会检查文件后辍,如果不是 .ps1 则放弃运行。
echo 'echo Hello' > a.txt
pwsh a.txtProcessing -File 'a.txt' failed because the file does not have a '.ps1' extension. Specify a valid PowerShell script file name, and then try again.新建文件,读写。
'Hello World
Hello Sun
Hello Moon
Hello Human
Hello Animals
Hello Robots' | Out-File a.txt
Get-Content a.txt
Get-Content a.txt -Head 3
Get-Content a.txt -Tail 3Hello World
Hello Sun
Hello Moon
Hello Human
Hello Animals
Hello Robots
Hello World
Hello Sun
Hello Moon
Hello Human
Hello Animals
Hello Robots新建文件,合并文件。
'Hello Air' | Out-File b.txt
Get-Content *.txt | Out-File c.md
Get-Content c.mdHello World
Hello Sun
Hello Moon
Hello Human
Hello Animals
Hello Robots
Hello Air删除文件第一行。
$content = Get-Content c.md | Select-Object -Skip 1
Set-Content c.md $content
Get-Content c.mdHello Sun
Hello Moon
Hello Human
Hello Animals
Hello Robots
Hello Air替换文件内容。
$content = Get-Content c.md | ForEach-Object { $_ -replace 'Hello', 'Hi' }
Set-Content c.md $content
Get-Content c.mdHi Sun
Hi Moon
Hi Human
Hi Animals
Hi Robots
Hi AirPowerShell 输出重定向有:
>覆盖文件>>追加文件>&1指定流重定向到 Success 流。
输入重定向 < ,表示读取文件输入。
新建文件,写入内容。
echo 'Hello World' > a.txt
echo 'Hello Human' >> a.txt
cat a.txtHello World
Hello Human覆盖内容。
echo 'Hello World' > a.txt
cat a.txtHello World