Reputation: 335
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.
What am I doing wrong?
Upvotes: 0
Views: 2032
Reputation: 438273
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.
Upvotes: 3
Reputation: 26
This will work:
Get-ChildItem -Recurse | select FullName,Length,LastAccessTime | Export-Csv list.csv
Upvotes: 1