Working with Text Files

Code Properties

  • Language: PowerShell
  • Cmdlets: Get-Content, Set-Content, Select-String

Overview

PowerShell has built-in commands for interacting with text files. These commands provide powerful control over file operations with familiar aliases for users coming from Unix/Linux.

Commands

CommandAliasDescription
Get-ContentcatRead file contents
Select-StringgrepSearch for patterns
Set-Content-Write to file
Sort-ObjectsortSort content
Measure-ObjectmeasureCount lines/words
Select-Object -First-Get first N lines
Select-Object -Last-Get last N lines

Code

Read File Contents

# read entire file
Get-Content test.log
 
# read first 10 lines
Get-Content test.log -Head 10
 
# read last 10 lines
Get-Content test.log -Tail 10
 
# monitor file changes (like tail -f)
Get-Content test.log -Wait -Tail 10

Search File Contents

# search for pattern
Select-String -Path test.log -Pattern "error"
 
# case-insensitive search
Select-String -Path test.log -Pattern "error" -CaseSensitive:$false
 
# search multiple files
Select-String -Path "*.log" -Pattern "warning"

Write to Files

# overwrite file
"Hello World" | Set-Content output.txt
 
# append to file
"New line" | Add-Content output.txt
 
# write array to file
@("Line 1", "Line 2", "Line 3") | Set-Content output.txt

Count and Measure

# count lines
(Get-Content test.log | Measure-Object -Line).Lines
 
# count words
(Get-Content test.log | Measure-Object -Word).Words
 
# count characters
(Get-Content test.log | Measure-Object -Character).Characters

Usage

# read log file, filter errors, sort, and save
Get-Content app.log | 
    Select-String "ERROR" | 
    Sort-Object | 
    Set-Content errors.txt

Appendix

Note created on 2024-09-25 and last modified on 2024-12-31.

See Also


(c) No Clocks, LLC | 2024