问题描述
我在 Powershell Core 中运行了以下 Powershell 脚本:
I have the following Powershell script that I am running in Powershell Core:
PS C:\> Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10
>> files.txt
打开文件 files.txt 时,LastAccessTime、LastWriteTime 没有列 - 为什么会这样,我该如何修改脚本以便将它们包含在文本文件中?
Opening the file files.txt there are no columns for LastAccessTime, LastWriteTime - why is this and how do I amend the script so they are included in the text file?
推荐答案
两点:
>>
是Out-File -Append
的(虚拟)别名;如果意图不是追加(到预先存在的文件)而是只捕获当前命令的(整个)输出,请仅使用>
/Out-File
没有-Append
.
>>
is a (virtual) alias forOut-File -Append
; if the intent is not to append (to a preexisting file) but to only capture the current command's (entire) output, use just>
/Out-File
without-Append
.
Out-File
并且因此也 >
/>>
捕获 for-displayem> 文件中输入对象的表示,不适合后续的程序化处理.对于后者,请使用生成结构化输出的 cmdlet,例如 Export-Csv
.
Out-File
and therefore also >
/ >>
capture the for-display representation of the input objects in a file, which is not suitable for subsequent programmatic processing. For the latter, use a cmdlet that produces structured output, such as Export-Csv
.
也就是说,考虑到 file 而不是显示,期望 Out-File
捕获所有 for-display 数据是合理的是目标.
That said, it's reasonable to expect Out-File
to capture all for-display data, given that a file rather than the display is the target.
遗憾的是,从 PowerShell [Core] v7.0 开始,Out-File
/>
/>>
是(仍然) 对控制台(终端)窗口的宽度敏感,并且仅捕获当前适合屏幕的宽度.
Regrettably, as of PowerShell [Core] v7.0, Out-File
/ >
/ >>
are (still) sensitive to the console (terminal) window's width and capture only as much as would currently fit on the screen.
您可以使用 Out-File
使用 -Width
参数来解决这个问题:
You can use Out-File
with the -Width
parameter to work around that:
Get-ChildItem -Recurse -File -Filter *.pbix |
Sort-Object -Property LastAccessTime |
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 |
Out-File -Width ([int]::MaxValue-1) files.txt # Add -Append, if needed.
这篇关于为什么所有的 Powershell 输出都没有写入 txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!