Using the Get-Content Cmdlet

Reading a Text File

What can you do with the Get-Content cmdlet? Well, one thing you can do is quickly read and display the contents of a text file. For example, this command displays the contents of the file C:\Scripts\Test.txt:

Get-Content c:\scripts\test.txt

And here’s what you’ll get:

Hey, we never said it was exciting, just that it was useful.

How useful? Here’s a simple, but telling example. Suppose you have a text file (C:\Scripts\Test.txt) that contains the names of two or more computers. You’d like to use WMI to retrieve information about the BIOS installed on each of those computers. Here you go:

Get-Content c:\scripts\test.txt | Foreach-Object {Get-Wmiobject -computername $_ win32_bios}

Assuming you have just two computers list in the text file you’ll get back information similar to this:

SMBIOSBIOSVersion : 68DTT Ver. F.0D
Manufacturer      : Hewlett-Packard
Name              : EPP runtime BIOS - Version 1.1
SerialNumber      : CND60723S7
Version           : HP     - 22110520

SMBIOSBIOSVersion : A03
Manufacturer      : Dell Computer Corporation
Name              : Phoenix ROM BIOS PLUS Version 1.10 A03
SerialNumber      : HTVNX41
Version           : DELL   - 7

Good question: how does that command work? The first part is pretty self-explanatory: we just use Get-Content to read the contents of the text file. Now, let’s take a look a look at the second half of the command:

ForEach-Object {Get-Wmiobject -computername $_ win32_bios}

What we’re doing here is passing the contents of the text file (our computer names) to the ForEach-Object cmdlet. As it turns out, Get-Content automatically creates an array based on the content it is retrieving; in the case of a text file, each line in the file will be a single item in the array. The ForEach-Object simply takes the contents of the file, one line at a time, and calls the Get-WmiObject cmdlet, providing Get-Wmiobject with a different line from the file (using the default pipeline variable $_) repeatedly until all lines have been read. Get-Wmiobject then connects to the computer and retrieves information from the Win32_BIOS class.

Confused? Try it on your set of computers and see what happens.

Because Get-Content automatically creates an array consisting of each line in the file, that means you can also use the Measure-Object cmdlet to easily count the number of lines in the file, a task people like to do on a regular basis:

Get-Content c:\scripts\test.txt | Measure-Object

The preceding command returns data similar to this:

Count    : 124
Average  :
Sum      :
Maximum  :
Minimum  :
Property :

Interesting question: what if you want to return only the first x number of lines in the file? In that case simply add the -totalcount parameter followed by the number of lines to retrieve. This command returns only the first five lines in the file Test.txt:

Get-Content c:\scripts\test.txt -totalcount 5

To get the last five lines in the text file simply read the file using Get-Content, then have Select-Object pick out the last five items for you:

Get-Content c:\scripts\test.txt | Select-Object -last 5
Get-Content Aliases
  • gc

  • type

  • cat