This is a test website to try out GitHub and Git Bash
01 Jun 2022 - jhunter
get-help get-content
get-content
more
, gc
, and cat
Read a file.
-Tail
allows you to only see the last x lines of a file.
Get-content
lets you simply read a file, but I tend to use it to work with a list of (a little) data that originated from outside of PowerShell.
In this example I save a list in text file, then use get-content
to put the list into a variable.
$list = get-content .\test.txt
In this way I can loop through the data with other commands.
foreach($pc in $list) {write-host "PC is : $pc" -ForegroundColor yellow}
Here I use -Tail
with an infinite loop to read (or “watch”) a log file where I expect to see changes.
while($true){cls ; Get-Content .\testlog.txt -Tail 5 ; Start-Sleep 5 }
The alias more
is not an exact alias of get-content
because more
will show part of the file content if that content is longer than the screen height (this is called “pagination”).
In this way more
can be used in two ways:
get-content ./mylongfile.txt | more
more ./mylargefile.txt