如何在PowerShell中获取包含文件内容的文件名

如何在PowerShell中获取包含文件内容的文件名,powershell,Powershell,我有大约50个文件,我要合并成一个文件名,然后文件内容,然后在文件输出后留下一行,可能是虚线,例如,它应该是这样的 File name -ABC xxxxxxxxxxxxxxxx (Content of the file) ..................... (dotted line after output) File Name - CDE xxxxxxxxxxxx (Content of the file) ................... 这个脚本给我的输出不是我想要的格式

我有大约50个文件,我要合并成一个文件名,然后文件内容,然后在文件输出后留下一行,可能是虚线,例如,它应该是这样的

File name -ABC xxxxxxxxxxxxxxxx (Content of the file) ..................... (dotted line after output) File Name - CDE xxxxxxxxxxxx (Content of the file) ...................
这个脚本给我的输出不是我想要的格式。我找不到获取文件名的方法。

您想要的是非常简单的。您只需要一个
ForEach对象
循环来单独处理每个输入文件,还需要格式化操作符(
-f
)将数据注入模板字符串:

Get ChildItem'C:\temp'| ForEach对象{
@'
文件名-{0}
{1}
.....................
'@-f$\.Name,(获取内容$\.FullName-Raw)
}|输出文件“C:\path\to\output.txt”

你想要的东西相当微不足道。您只需要一个
ForEach对象
循环来单独处理每个输入文件,还需要格式化操作符(
-f
)将数据注入模板字符串:

Get ChildItem'C:\temp'| ForEach对象{
@'
文件名-{0}
{1}
.....................
“@-f$.Name,(获取内容$.FullName-Raw)
}|输出文件“C:\path\to\output.txt”