用 PowerShell 把多个文本文件合并成一个
用 Get-Content + Set-Content 一条命令把多个 txt/csv/log 文件按文件名顺序合并,也可用 cmd 的 type/copy 实现。
运行 Get-Content 'C:\Logs\part-*.txt' | Set-Content 'C:\Logs\merged.txt',即可按文件名顺序合并所有匹配文件。
01命令
命令
$ Get-Content 'C:\Logs\part-*.txt' | Set-Content 'C:\Logs\merged.txt'02作用说明
Get-Content 支持通配符,会按字母顺序逐行读取所有匹配的文件,通过管道交给 Set-Content 写进一个新文件。如果想在文件之间加分隔标题,可以改用循环:Get-ChildItem *.txt | ForEach-Object { "=== $($_.Name) ==="; Get-Content $_ } | Set-Content merged.txt。不用 PowerShell 的话,在 cmd 里 copy /b part-*.txt merged.txt 或 type part-*.txt > merged.txt 也能完成同样的合并。
03风险
!注意: 输出文件不要和输入通配符同名(比如输出也叫 part-*.txt),否则下次会把合并结果再合并一遍。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Get-Content依据: Get-Content with wildcards reads the line contents of all matching files in order, ready to pipe into a single output file.
- Get-Content - SS64依据: Demonstrates piping Get-Content output of multiple files into a combined file.
最后验证: 2026-09-18 · 分类: 文件与文件夹