Powershell get-childitem output formatting

32,298

Solution 1

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

Solution 2

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
Share:
32,298
Root Loop
Author by

Root Loop

Updated on February 17, 2020

Comments

  • Root Loop
    Root Loop about 4 years

    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\
    
  • Root Loop
    Root Loop about 10 years
    Thanks for comments, but it wont work.....I tried select-object already.... the resault still the same....
  • mjolinor
    mjolinor about 10 years
    Can you post the script where you trid that? I've never seen that not work.
  • Root Loop
    Root Loop about 10 years
    I tried the same code you post above, it gave me the same result . the format wont change at all. I am thinking I may have to use css file to work with it which i dont want to make it too complx......
  • mjolinor
    mjolinor about 10 years
    I can't reproduce that result.
  • Hunter Eidson
    Hunter Eidson about 10 years
    The select worked for me on my box, although the directory got truncated since I was using a longer path as my test...
  • mjolinor
    mjolinor about 10 years
    If it's truncated, that the defalt format-table doing that. Try piping the result through format-list, or format-table -autosize