Skip to content

文件

PowerShell 运行文件时会检查文件后辍,如果不是 .ps1 则放弃运行。

echo 'echo Hello' > a.txt

pwsh a.txt

新建文件,读写

'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 3

新建文件,合并文件

'Hello Air' | Out-File b.txt

Get-Content *.txt | Out-File c.md

Get-Content c.md

删除文件第一行

$content = Get-Content c.md | Select-Object -Skip 1

Set-Content c.md $content

Get-Content c.md

替换文件内容

$content = Get-Content c.md | ForEach-Object { $_ -replace 'Hello', 'Hi' }

Set-Content c.md $content

Get-Content c.md

联系 math@baima.site