I'm aware that Get-Content has a -Tail parameter, but as far as I can tell, there's simply no way to get it to accept piped input.
So how do I get something like this to work? I just want to be able to select first X or last X lines from any preceding command, without the hassle of creating temp files simply so I can grab the first/last lines from it.
# This won't work as Get-Content demands a -Path to a file
Get-Item .\* | Get-Content -Tail 10
Some added finagling will likely be required depending on whether the first command is providing objects or strings as output; if you can include such intermediary steps in your reply, that'd be awesome. I expect something like Out-String -stream might be the done thing?
I've scoured the internet and came up empty-handed. Thanks in advance for any help.
PowerShell's pipeline is based on objects rather than lines of text, and Mathias R. Jessen's helpful answer has the object-oriented angle covered:
Select-Object-First/-Lastselects only the first / last N objects - whatever their type - and then renders them to the display using PowerShell's rich for-display output-formatting system.Given that the formatting of a single object can span multiple lines (consider the display output when you submit
$PSVersionTable), there's no guarantee that the output will be limited to N lines.If, by contrast, your intent is to limit the output to a given number of display lines - irrespective of how many objects the result corresponds to:
Insert an
Out-Stringcall with the-Streamswitch parameter before theSelect-Objectcall. PowerShell v5+ even has a built-in wrapper function for this,oss.This performs the for-display formatting to a string, with
-Streamthen emitting the lines of the result one by one.A simple example that illustrates the two approaches and how they differ:
This outputs the following:
Note:
PowerShell's output formatting often involves a leading and trailing empty line, which is why the output from the
oss-based command starts with an empty line and only shows one property value (you could address that by eliminating blank lines as follows:$objects | oss | Where-Object { $_.Trim() } | Select-Object -First 2)An important use case for
ossis quick-and-dirty text searches in object output viaSelect-String:E.g., the following would show only the lines that contain the digit (string)
3:Where-Object, is the right approach.Sadly,
Select-Stringdoesn't have theossstringification logic built in, even though it arguably should - see GitHub issue #10726.