What is the Get-Content method in PowerShell?

Overview

The Get-Content cmdlet is used to read the content from a file at a specified location using PowerShell. It reads line by line and returns the collection of objects.

Syntax

Get-Content -Path "path to the file"

Parameters

We need to pass the Path to a file as a mandatory parameter. It also accepts the following other parameters:

  • TotalCount: This is used to limit the number of lines Get-Content returns.
  • Tail: This is used to get the last line of the file.

Return value

This cmdlet returns a collection of objects, each representing a line of content in the file.

Example

example.ps1
data.txt
Educative helps software engineers grow their skill sets and reach their full potential.
Text-based courses with interactive in-browser coding helps you learn faster and smarter.
In-browser exercises. Free Sign-Up. No setup required. Learn at your own pace.

Explanation

In the above code snippet, we read the content of the file data.txt using the cmdlet Get-Content on line 2.

Free Resources