How to Use Get-Content in Powershell

How to Use Get-Content in Powershell

Whether you're automating tasks, processing logs, or handling configuration files, reading file contents is a frequent requirement. PowerShell's Get-Content cmdlet is the built-in tool for this.

It provides a flexible and script-friendly way to read files line by line, all at once, or in chunks - without requiring any complex logic or external libraries.

What is the Get-Content Cmdlet?

The Get-Content cmdlet reads the contents of a file or other supported data sources such as the registry or alternate data streams. It outputs content as an array of strings (by default, one per line), but it also supports modes that return single strings, byte arrays, or stream content - depending on your use case.

You can also use dynamic parameters depending on the provider - for example, a Path parameter on the FileSystem provider or Encoding on a custom cmdlet. It’s a powerful method to read file contents efficiently with full control over performance, memory usage, and output format.

You’ll use Get-Content command when you need to:

  • Read log files line-by-line
  • Pull in data from CSV files, XML files, or configuration files
  • Monitor files dynamically (e.g., tailing)
  • Process file contents through the pipeline

Syntax

Get-Content

[-Path] <String[]>

[-Credential <PSCredential>]

[-Delimiter <Char>]

[-Encoding <Encoding>]

[-Exclude <String[]>]

[-Filter <String>]

[-Force]

[-Include <String[]>]

[-PathType <PathType>]

[-ReadCount <Int64>]

[-Raw]

[-AsByteStream]

[-Stream <String>]

[-Tail <Int64>]

[-TotalCount <Int64>]

[-Wait]

[-Skip <Int64>]

[-First <Int64>]

[-Index <Int32[]>]

[-SkipLast <Int32>]

[<CommonParameters>]

Parameters

Here's brief explanation of some selected parameters:

  • -Path: Specifies the path to one or more files. Supports wildcard characters and file system drives.
  • -Raw: Returns the entire content as a single undelimited string instead of an array of lines. Useful for reading JSON, XML, or configuration files in one go.
  • -Encoding: Specifies the type of file encoding (e.g., ASCII, UTF-8, UTF-16). Crucial when reading non-default encoded files to avoid character corruption.
  • -Delimiter: Used when reading from a file stream to break input based on a specific character. Especially useful for processing large CSV files.
  • -Wait: Keeps the file open and returns content as it's added. Ideal for monitoring real-time updates in a log file.
  • -PathType: Useful for specifying whether you want to retrieve directories only, files only, or all paths.
  • -ReadCount: Reads a set number of lines at a time. Helps manage memory footprint with larger files.
  • -AsByteStream: Reads file content as a stream of bytes instead of strings. Best when working with binary files.
  • -Tail: Reads the last N lines of a file. Mimics tail functionality from Unix-like systems.
  • -Index: Returns specific lines based on their zero-based index position. Useful for selectively accessing entries.

These parameters let you control output format, encoding type, memory usage, and live file reading behavior. The cmdlet also supports pipeline input and outputs objects you can work with using other cmdlets like Select-Object, Where-Object, or ForEach-Object.

Practical Uses

  1. Monitoring Application Logs
    Use the Tail and Wait parameters to follow a real-time stream of log entries during application troubleshooting.
  2. Parsing Configuration Files
    Read a module manifest file, INI-style config, or CSV files into memory for validation or automation purposes.
  3. Reading File Chunks for Memory Management
    Break up large files into manageable segments using -ReadCount or -TotalCount to process larger files without overwhelming system memory.

Prerequisites

Before using the Get-Content cmdlet effectively, confirm the following:

  • PowerShell 5.1 or later is recommended. All examples in the Microsoft documentation assume you're on a modern version (PowerShell 7.5+).
  • You must have read access to the file path specified. Lack of permission will throw an access denied error.
  • The file path must point to a valid file system drive, not just a string. It does not work on Registry or certificate drives.
  • When using -Credential, remote file access via file shares must be properly configured.
  • To use -AsByteStream, -Index, -SkipLast, or -Delimiter, make sure you're on PowerShell 7.0+, as these parameters do not exist in earlier versions.
  • If you plan to monitor file changes with -Wait, the file must be on a drive that supports real-time update detection (e.g., not cloud sync folders like Dropbox or OneDrive).
  • File Type: Supports plain text, XML, JSON, or any format that stores content as lines or bytes. If you're working with binary files or need to inspect file in bytes, use the -AsByteStream parameter.
  • Memory Considerations: Large files are read into memory line-by-line by default. If you're processing large log files or reading file into objects in blocks, use -ReadCount or -Raw for more efficient memory usage.

How to Use Get-Content: 6 Examples

Here is a practical, step-by-step breakdown of how to use the Get-Content cmdlet, with clear examples of parameter usage and why each step matters.

Example 1: Read the contents of a file line-by-line

This is the most common usage. You’re accessing a standard text file, such as logs or configuration files.

Get-Content -Path C:Logsapp.log

This returns each line as a separate string object in an array. It's memory-efficient for small to medium-sized files and plays well with loops like ForEach-Object.

Example 2: Get the entire file as a single string

To reduce processing overhead or when working with JSON or XML that must remain intact, use the -Raw parameter.

Get-Content -Path C:Configssettings.json -Raw

This outputs the entire content as one undelimited string (a single object). It minimizes memory allocation and is faster than line-by-line reads for small files.

Example 3: Read binary files using a byte array

When working with images, executables, or files in binary format, use -AsByteStream. This avoids string conversion and gives you full control of the raw bytes.

Get-Content -Path C:Imageslogo.png -AsByteStream

This returns the file as a byte array, allowing you to pass it to hashing functions or write it elsewhere using Set-Content -AsByteStream.

Example 4: Tail the last N lines of a log file

To analyze the most recent events without reading an entire log file, use the -Tail parameter.

Get-Content -Path C:Logsserver.log -Tail 50

This retrieves the last 50 lines, avoiding memory waste and speeding up response when working with larger files.

Example 5: Wait for live updates in real-time

To monitor changes as they happen in an active log file, use the -Wait switch.

Get-Content -Path C:Logslive.log -Wait

This keeps the file open and streams new lines as they're written. Combine this with Select-String to filter live updates.

Example 6: Use specific encoding when reading files

If a file is encoded in a non-default format, you must specify the correct encoding to avoid corrupted output or misread characters.

Get-Content -Path C:Dataunicode.txt -Encoding UTF8

Common encoding values include UTF8, UTF7, ASCII, UTF32, BigEndianUnicode, and Default. Always verify the file’s encoding if the output contains invalid characters.

The Get-Content cmdlet is essential for any PowerShell user working with files, whether you're pulling in CSV files, reading logs, or parsing configuration files. It supports everything from single-string output to byte streams, and it handles common issues like file encoding, newline characters, and memory constraints through its parameters.

Use it alongside cmdlets like Select-String, ForEach-Object, and Add-Content to build complete pipelines that can read, filter, and process content with precision.

Understanding its full range - especially flags like -Raw, -Wait, and -AsByteStream - is critical if you're dealing with larger files, file in chunks using memory limits, or need reliable content checks. Learn to combine Get-Content with tools like Select-Object or Compare-Object to go even deeper.If you're reading a single file, working with an array of strings, or piping contents into a loop, make Get-Content the first cmdlet you reach for.