3

I have the following Powershell script that I am running in Powershell Core:

 PS C:\> Get-ChildItem -Recurse -File -Filter *.pbix | 
Sort-Object -Property LastAccessTime | 
Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 
>> files.txt

Opening the file files.txt there are no columns for LastAccessTime, LastWriteTime - why is this and how do I amend the script so they are included in the text file?

whytheq
  • 34,466
  • 65
  • 172
  • 267
  • 1
    I'd recommend to use CSV files for structured data instead of plain text. Take a look at the cmdlet `Export-Csv`. – Olaf Jun 28 '20 at 22:41
  • 3
    `Out-File` is limited to the amount of space you have on your screen, if your screen does not fit, it will not show all of the data, try storing the output to a variable like `$test = Get-ChildItem -Recurse -File -Filter *.pbix | Sort-Object -Property LastAccessTime | Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10`, then you can access any of the data with `$test.LastWriteTime` for instance. – Nico Nekoru Jun 28 '20 at 22:42

1 Answers1

4

Two asides:

  • >> is a (virtual) alias for Out-File -Append; if the intent is not to append (to a preexisting file) but to only capture the current command's (entire) output, use just > / Out-File without -Append.

  • Out-File and therefore also > / >> capture the for-display representation of the input objects in a file, which is not suitable for subsequent programmatic processing. For the latter, use a cmdlet that produces structured output, such as Export-Csv.

That said, it's reasonable to expect Out-File to capture all for-display data, given that a file rather than the display is the target.

Regrettably, as of PowerShell [Core] v7.0, Out-File / > / >> are (still) sensitive to the console (terminal) window's width and capture only as much as would currently fit on the screen.

You can use Out-File with the -Width parameter to work around that:

Get-ChildItem -Recurse -File -Filter *.pbix |
  Sort-Object -Property LastAccessTime |
    Select-object -Property Name, FullName, LastAccessTime, LastWriteTime -Last 10 |
      Out-File -Width ([int]::MaxValue-1) files.txt # Add -Append, if needed.

Note: As of v7.0, [int]::MaxValue-1 is seemingly the highest value accepted; [int]::MaxValue is quietly ignored. Also, be sure not to use such a high value in Windows PowerShell, where each output line is unconditionally right-space-padded to that length, which would result in excessively large files.

mklement0
  • 382,024
  • 64
  • 607
  • 775