更新于 2026-01-26
Get-Content
$lines = Get-Content -Path 'C:\path\to\file.txt'
读取所有行,返回字符串数组。
System.IO.File::ReadAllText
$text = [System.IO.File]::ReadAllText('C:\path\to\file.txt')
一次性读取所有文本为一个字符串。
System.IO.File::ReadAllLines
$lines = [System.IO.File]::ReadAllLines('C:\path\to\file.txt')
读取所有行到字符串数组。
Set-Content
'内容' | Set-Content -Path 'C:\path\to\file.txt'
覆盖写入文本。
Out-File
'内容' | Out-File -FilePath 'C:\path\to\file.txt' -Encoding utf8
System.IO.File::WriteAllText
System.IO.File::WriteAllLines
$lines = @('line1', 'line2')
[System.IO.File]::WriteAllLines('C:\path\to\file.txt', $lines)
'追加内容' | Add-Content -Path 'C:\path\to\file.txt'
'追加内容' | Out-File -FilePath 'C:\path\to\file.txt' -Append
$jsonObj = Get-Content 'file.json' | ConvertFrom-Json
$jsonObj | ConvertTo-Json | Set-Content 'file.json'