简体   繁体   English

Powershell get-childitem输出格式

[英]Powershell get-childitem output formatting

How can I change the formatting of powershell output? 如何更改powershell输出的格式?
I am running this: 我正在运行这个:

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\

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 formatting question, 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 -Wrap or -AutoSize to the end of the format-table cmdlet. 我能看到的唯一缺点是如果目录名最终太长,您可能需要尝试将-Wrap-AutoSize添加到format-table 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

You can re-order the properties with Select-Object (select): 您可以使用Select-Object (select)重新排序属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM