Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell get-childitem output formatting

How can I change the formatting of powershell output?
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\
like image 839
Root Loop Avatar asked Feb 13 '14 19:02

Root Loop


People also ask

What is the output of Get-ChildItem?

The Get-ChildItem cmdlet uses the Path parameter to specify the directory C:\Test . Get-ChildItem displays the files and directories in the PowerShell console. By default Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item.

How do you get the full path of ChildItem?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

What is Get-ChildItem in PowerShell?

Get-ChildItem displays the files and directories in the PowerShell console. By default, Get-ChildItem lists the mode (Attributes), LastWriteTime, file size (Length), and the Name of the item. The letters in the Mode property can be interpreted as follows: l (link)

How do I use AutoSize in PowerShell?

If you specify the AutoSize parameter when you run the Format-Table command, PowerShell calculates column widths based on the actual data displayed. This makes the columns readable. The Format-Table cmdlet might still truncate data, but it only truncates at the end of the screen.


2 Answers

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:

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.

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:

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
like image 122
Hunter Eidson Avatar answered Sep 21 '22 03:09

Hunter Eidson


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

gci -Recurse K:\AppData\*.* -Filter *.model | 
? {$_.LastWriteTime -gt (Get-Date).AddDays(-6)} | 
sort LastWriteTime -descending |
Select LastWriteTime,Name,Directory
like image 41
mjolinor Avatar answered Sep 21 '22 03:09

mjolinor