~/windows-reading-files

Reading Files

CPTS Notes4 min read

Get-Content prints files, with -TotalCount and -Tail for the first or last lines. PowerShell pipes objects, not text, so Select-Object, Sort-Object, and -split take the place of cut, sort, and awk. Chain them with | to turn command output and config files into lists.

Get-Content

Print a whole file (cat, gc, and type are aliases):

Get-Content C:\Users\bob\notes.txt # whole file to the screen

First or last lines:

Get-Content app.log -TotalCount 20 # first 20 lines
Get-Content app.log -Tail 20 # last 20 lines

Follow a file as it grows (like tail -f):

Get-Content app.log -Tail 10 -Wait # print new lines as they arrive

Page through a long file (q quits):

Get-Content app.log | more # scroll through the file
Flag Meaning
-TotalCount N Read only the first N lines (aliases -Head, -First)
-Tail N Read only the last N lines (alias -Last)
-Wait Keep the file open and print new lines as they arrive
-Raw Read the whole file as one string, not an array of lines

Get-Content returns an array of lines, so (Get-Content file).Count counts them and (Get-Content file)[0] is the first line. -Raw gives one string instead, which regex and parsers often need.

Picking columns

PowerShell splits a line with the -split operator, which takes a regex:

(Get-Content hosts.txt) | ForEach-Object { ($_ -split '\s+')[1] } # second whitespace field of each line

Split on a set character, such as a CSV comma:

Get-Content users.csv | ForEach-Object { ($_ -split ',')[0] } # first comma field

For real structured data, parse it instead of splitting. ConvertFrom-Csv and Import-Csv give named columns:

Import-Csv users.csv | Select-Object -ExpandProperty Email # the Email column

Select-Object picks object properties, the object-world version of choosing columns:

Get-Process | Select-Object Name, Id, Path # three properties of each process
Operator Meaning
-split 'regex' Split a string into parts on a regex
($x -split ',')[0] The first part, index 0
Select-Object -ExpandProperty P The raw value of property P, not a wrapped object

An aligned-column text file splits on '\s+' (one or more spaces). A delimited file splits on its delimiter, or parses with ConvertFrom-Csv -Delimiter.

Sorting and deduping

Sort-Object orders lines, and -Unique drops duplicates:

Get-Content users.txt | Sort-Object -Unique # sorted, no duplicates

Sort as numbers, not text:

Get-Content ports.txt | Sort-Object { [int]$_ } # numeric order

Count each value, most frequent first (the sort | uniq -c | sort -rn idiom):

Get-Content access.log | Group-Object | Sort-Object Count -Descending | Select-Object Count, Name
Command Meaning
Sort-Object Order items
Sort-Object -Unique Order and drop duplicates
Sort-Object -Descending Reverse order
Group-Object Group equal items, with a Count for each
Select-Object -First N / -Last N Keep the first or last N items

Sort-Object -Unique sorts and dedupes in one pass. Get-Unique only drops duplicates that are already next to each other, so it needs a sort first, like Linux uniq.

Joining and pipelines

Join lines into one string (the paste -sd, move, for an nmap -p port list):

(Get-Content ports.txt) -join ',' # all lines as one comma list

Pair two lists line by line as user:pass:

$u = Get-Content users.txt; $p = Get-Content pass.txt
0..($u.Count - 1) | ForEach-Object { "$($u[$_]):$($p[$_])" } # line n of each, joined by a colon

Build a target list from a command. Reachable hosts from a /24, one IP per line:

# hosts that answer a ping, saved to hosts.txt
1..254 | ForEach-Object { "10.129.2.$_" } | Where-Object { Test-Connection $_ -Count 1 -Quiet } | Tee-Object hosts.txt

Tee-Object writes to a file while it prints, like Linux tee (see Editing Files). Pull user:hash pairs out of a secretsdump file:

Select-String ':::' sam.txt | ForEach-Object { ($_ -split ':')[0,3] -join ':' } # user:nthash

Select-String is the grep of PowerShell. It and the rest of the search tools are in Searching Files. Credential stores to read on a host are in Looting (Windows) and Hunting (Windows).