windows Powershell get-childitem 输出格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21763692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:23:13  来源:igfitidea点击:

Powershell get-childitem output formatting

windowspowershellpowershell-2.0powershell-3.0

提问by Root Loop

How can I change the formatting of powershell output?
I am running this:

如何更改 powershell 输出的格式?
我正在运行这个:

cgi -Recurse K:\AppData\*.* -Filter *.model | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending >> C:\AAA\result.txt

The result I got is in this format:

我得到的结果是这种格式:

Directory: K:\AppData\


Mode                LastWriteTime     Length Name                                                                                                                                                                                                  
----                -------------     ------ ----                                                                                                                                                                                                  
-a---        13/02/2014  11:29 AM    7269129 20-300_3001_REV02_ECR4431.CATPart 
-a---        13/02/2014  11:29 AM    7269129 20-300_3001_REV02_ECR4431.CATPart 
-a---        13/02/2014  11:29 AM    7269129 20-300_3001_REV02_ECR4431.CATPart

How can I change the output format to this :

如何将输出格式更改为:

LastWriteTime           Name                                  Directory
-------------           ----                                  -----
 13/02/2014  11:29 AM   20-300_3001_REV02_ECR4431.CATPart     K:\AppData\
 13/02/2014  11:29 AM   20-300_3001_REV02_ECR4431.CATPart     K:\AppData\
 13/02/2014  11:29 AM   20-300_3001_REV02_ECR4431.CATPart     K:\AppData\

回答by Hunter Eidson

I know the usual answer is, don't use the format-* cmdlets, since the output can't really be used by other cmdlets, but since this a formattingquestion, how about something like:

我知道通常的答案是,不要使用 format-* cmdlet,因为其他 cmdlet 不能真正使用输出,但是由于这是一个格式问题,所以如何:

get-childitem -Recurse K:\AppData\*.* -Filter *.model | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | format-table LastWriteTime, Name, Directory >> C:\AAA\result.txt

The only downside I can see is if the directory name ends up being too long, you may need to try playing with adding either -Wrapor -AutoSizeto the end of the format-table cmdlet.

我能看到的唯一缺点是如果目录名称最终太长,您可能需要尝试添加-Wrap或添加-AutoSize到格式表 cmdlet 的末尾。

If neither of those solves the width issue (assuming you even have one), I found a (page)[http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/] about creating really wide tables, so you could end up with something like:

如果这些都不能解决宽度问题(假设你甚至有一个),我找到了一个(页面)[http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with -powershell/] 关于创建非常宽的表,所以你最终可能会得到类似的结果:

get-childitem -Recurse K:\AppData\*.* -Filter *.model | ? {$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | sort LastWriteTime -descending | format-table LastWriteTime, Name, Directory -AutoSize | Out-String -Width 1024 >> C:\AAA\result.txt

回答by mjolinor

You can re-order the properties with Select-Object(select):

您可以使用Select-Object(选择)重新排序属性:

gci -Recurse K:\AppData\*.* -Filter *.model | 
? {$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | 
sort LastWriteTime -descending |
Select LastWriteTime,Name,Directory