0

I'm trying this command in Windows 10 PowerShell:

Get-ChildItem -Recurse | select FullName,Length,LastAccessTime

The result only contains FullName , while LastAccessTime and Length is missing from output.

PowerShell ScreenShot

What am I doing wrong?

Nandha
  • 192
  • 1
  • 2
  • 11
  • 3
    The same command works for me (tested on versions 5 and 7). Maybe the output is just offscreen somewhere. – OwlsSleeping Dec 20 '20 at 14:39
  • I wrote the output to a file using ```Get-ChildItem -Recurse | select FullName,Length,LastAccessTime | Out-File list.txt ```. It's only shows filenames. Also tried using Format Table but still nothing – Nandha Dec 20 '20 at 15:17
  • Try: `... | Format-Table -Property @{expression='ps*'; width=10}` – iRon Dec 20 '20 at 15:21
  • [Try this](https://stackoverflow.com/a/36516106/3439404): `…| Out-String -Width 1000 | Out-File list.txt` – JosefZ Dec 20 '20 at 15:30
  • 3
    What if you put fullname last – Doug Maurer Dec 20 '20 at 15:32
  • it returns empty output – Nandha Dec 20 '20 at 15:32
  • 2
    Instead of Out-File, use `Export-Csv` or output the objects using `Out-GridView` – Theo Dec 20 '20 at 16:12
  • What if you use the actual `Select-Object` cmdlet (instead of its alias) or just: `Get-ChildItem -Recurse | Format-Table FullName,Length,LastAccessTime`? – iRon Dec 20 '20 at 16:31
  • 1
    'Export-Csv' solutions works. @iRon I think, the problem is due to length of filenames. Thanks for the help Guys. – Nandha Dec 20 '20 at 16:37
  • 1
    which is why putting it last should've shown all 3, with fullname cut off. – Doug Maurer Dec 20 '20 at 17:13

2 Answers2

3

The problem is merely a display problem:

  • Because the paths among the output objects are long and FullName is the first property selected, the remaining properties (columns) don't print, because they cannot fit on the screen. However, the properties are present, and can be used programmatically.

  • Note: If the intent is to save to a file for later programmatic processing, you shouldn't use > / Out-File, which result in the same for-display formatting that you see in the console (terminal), because this formatting is meant only for the human observer.


Workarounds:

  • A quick-and-dirty workaround is to put the FullName property last, as Doug Maurer advises, which will make all properties (columns) show, though the FullName property values will be truncated (symbolized with ), and notably from the start of the paths:

    # FullName is placed *last*
    # Caveat: Paths will be truncated at the *end*.
    Get-ChildItem -Recurse | select Length, LastAccessTime, FullName
    
  • If you don't mind switching to a list-based format, where each property value is represented on its own line prefixed by the property name, pipe to Format-List; note that overly long values will line-wrap:

    Get-ChildItem -Recurse | select FullName,Length,LastAccessTime | Format-List
    
  • If you do want tabular output and don't mind line-wrapping in your output, you can pipe to Out-String with a -Width value large enough to fit all columns (note that Out-File also supports -Width):

    Get-ChildItem -Recurse | select FullName,Length,LastAccessTime |
      Out-String -Width 300
    
    • If you prefer horizontal scrolling to line wrapping, you could save the above to a file and open in it a text editor or, with a text editor such as Visual Studio Code, you can directly pipe the output to it:

      # Opens the output directly in a new Visual Studio Code editor tab.
      Get-ChildItem -Recurse | select FullName,Length,LastAccessTime |
        Out-String -Width 300 | code -
      
  • Otherwise - if you do want to see tabular display in the console, in the exact column order specified and without line-wrapping - your only option is to truncate the FullName values so that all columns can fit; note that, for brevity, I am omitting the select (Select-Object) call in favor of direct use of Format-Table:

    Get-ChildItem -Recurse |
      Format-Table @{ e='FullName'; w=[Console]::WindowWidth-45; a='right'},
                   Length, LastAccessTime
    
    • Note how the column width (w entry, short for width) is based on the console window's with ([Console]::WindowWidth) minus 45 characters, to show as much of the FullName values as possible while still leaving room for the other two columns; also, to ensure that truncation (symbolized with is applied to the start of the path - so that file/directory name is always displayed - the column is right-aligned (via the a entry, short for alignment); if you prefer truncation of the end of the path, omit the a entry (which then defaults to left).

    • For more information on this hashtable-based (@{ ... }) way of specifying so-called calculated properties for Format-Table, including a way to truncate from the start while also maintaining left alignment, see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775
1

This will work:

Get-ChildItem -Recurse | select FullName,Length,LastAccessTime | Export-Csv list.csv