Search notes:

PowerShell cmdLet Get-Content

get-content retrieves the content of an item at a specified location.

Files

Usually, get-content is used to read a file. In such a case, get-content returns an array of strings (each of which is a text line in the file):
PS C:\Users\Rene> $fileContent = get-content README.txt
PS C:\Users\Rene> $fileContent.GetType().FullName
System.Object[]
PS C:\Users\Rene> $fileContent[0].GetType().FullName
System.String

Return a file's first/last n lines

The two parameters -first n and -last n return a file's first or last n lines:
get-content $profile -first 10
get-content $profile -last   4
Because these parameters do roughly what also the shell commands head and tail do, they have the synonyms -head and -tail
get-content -tail 20 app.log

Return one string with the content of the file rather than an array

In order to make get-content return the content of a file as a string rather than an array, the -raw option is needed:
PS C:\> $text = get-content -raw README.txt
Compare with the .NET method ReadAllText of the class System.IO.File

Grep for regular expressions

Because get-content returns a file as an array of strings, the array can be piped into where-object to search for specific lines that match a criteria (such as a regular expression).
PS C:\Users\Rene> get-content README.txt | where-object { $_ -match '\d\d\d' }

-readCount and -totalCount

The parameter -readcount specifies a number that controls the number of lines in the array that is passed down the pipeline.
The parameter -totalCount specifies the maximum number of lines that are read.

Processing every n-th line in a file

Thus, every n-th line in a file can be processed like so (here: n = 3):
get-content lines.txt -readCount 3 | foreach-object {
  write-host "Processing $($_[0])"
}
Every 2nd line, with a maximum of 3 processed lines (which correspond to 6 (=3*2) lines from the input file) can be achieved like so:
get-content lines.txt -readCount 2 -totalCount 6   | foreach-object {
   write-host "Processing $($_[0])"
}
In order to start with the n-th line rather than the first one, $_[0] must be changed accordingly.

Reading a file in batches of n bytes

In combination with -encoding byte («ordinary» PowerShell) or -asByteStream (PowerShell Core), -readCount and -totalCount allows to read a file in batches of n bytes:
if ($psVersionTable.psEdition -eq 'Desktop') {
   $threeTimesFourBytes = [byte[][]] (get-content $psCommandPath -encoding byte -readCount 4 -totalCount 12)
}
else {
   $threeTimesFourBytes = [byte[][]] (get-content $psCommandPath -asByteStream  -readCount 4 -totalCount 12)
}

foreach ($fourBytes in $threeTimesFourBytes) {
   '{0:x2} {1:x2} {2:x2} {3:x2}' -f @( $fourBytes )
}
Note, that the (supposed) array of four bytes must be enclosed into an array subexpression operator @(…) for format to work (see also my question on StackOverflow).

Getting the content of non-filesystem providers

It is possible to get the content non-filesystem providers such as the function provider:
PS C:\> get-content function:/mkdir

Variables as special kinds of items

Because a variable is just a special kind of an item, the value (that is: the content) of a variable can be determined with get-content.
The following example demonstrates that even indirection is possible: get-content variable:\$varName returns the value of the variable whose name is $varName:
$theNumber = 42

get-content variable:\theNumber
#
# 42

$varName = 'theNumber'

get-content variable:\$varName
#
# 42
Compare with get-variable that does not only return a variable's value but the System.Management.Automation.PSVariable object that is associated with the variable.

Functions

Functions are also types of items. Thus, the definition of a function can also be queried with get-content:
$mkdir_content = get-content function:\mkdir

$mkdir_content.GetType().FullName
#
# System.Management.Automation.ScriptBlock

#
# Print function definition
#
$mkdir_content.ToString()
Compare with get-command mkdir which returns a System.Management.Automation.FunctionInfo object.

Getting the content of a file as byte array

In order to get the content of a file as a byte array, get-content must be used together with the parameters -encoding byte -raw (PowerShell Desktop) or -asByteStream (PowerShell Core).

Hexadecimal representation of a file

The canonical way to get the content of a file in hexadecimal representation is the cmdLet format-hex.
format-hex also prints a header and the printable characters which sometimes is not what I want.
If the content of a file needs to be returned as a simple hexadecimal string, get-content along with -encoding byte and -raw might achieve this:
#
#  Get the content of a file as a byte array:
#
$arrayOfBytes = get-content -encoding byte -raw file.txt

$arrayOfBytes.GetType().FullName
#
#  System.Byte[]

#
#  Format each element of the byte array to a hexadecimal
#  representation, resulting in an array of strings, each
#  of which is two characters wide:
#
$arrayOfHex = $arrayOfBytes.foreach( { '{0:X2}' -f $_ } )

#
#  Join the elements of the string array to create
#  one string:
#
$hexString = $arrayOfHex -join ''

#
#  Print the string
#
$hexString
#
#  4142430A4445460A4748490A
Github repository about-PowerShell, path: /cmdlets/content/get/hexString.ps1
In PowerShell Core, there is no byte encoding anymore. The get-content -encoding byte needs to be replaced with get-content -asByteStream.

Reading XML data

XML data can be read with get-content by assigning it to a variable that is declared with the [xml] type accelerator.
[xml] $xml = get-content .\data.xml

$xml.root.items[2].item[2]
#
# baz
Github repository about-PowerShell, path: /cmdlets/content/get/read-xml.ps1
See also the PowerShell command noun XML.

data.xml

This was the XML test file I used for this example:
<root>

  <items id='1'><item>eggs</item> </items>
  <items id='3'><item>why</item></items>
  <items id='2'><item>foo</item><item>bar</item><item>baz</item></items>

</root>

The -encoding parameter

The -encoding parameter accepts a value of the Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding enum.

Alias

An (predefined) alias for get-content is cat (which is borrowed from the Unix shell command with the same name).
Another alias is type because of the cmd.exe command type.
Finally, there is the alias gc.

See also

Apparently, the item for which get-content is invoked needs to implement System.Management.Automation.Provider.IContentCmdletProvider.
The command parameter -credential.
Using get-content to pipe the content of a file with an expression into invoke-expression.
get-content belongs to the cmdlets with the -encoding parameter.
Powershell command noun: content

Index