~/windows-editing-files

Editing Files

CPTS Notes4 min read

The -replace operator rewrites text with a regex, and Set-Content writes the result back. Tee-Object saves output while you watch it. New-Item creates files, and a file’s timestamp properties set its times. These are the PowerShell forms of sed, tee, and touch.

Replace text

-replace substitutes every regex match in a string:

(Get-Content urls.txt) -replace 'http:','https:' # http: to https: on every line

Edit a file in place. Read it fully first, inside the parentheses, then write it back:

(Get-Content exploit.ps1) -replace '10\.10\.10\.10','10.10.14.5' | Set-Content exploit.ps1 # swap the IP

Keep a backup before an in-place edit:

Copy-Item exploit.ps1 exploit.ps1.bak # backup first
(Get-Content exploit.ps1) -replace '10\.10\.10\.10','10.10.14.5' | Set-Content exploit.ps1
Operator Meaning
-replace 'p','r' Replace every regex match of p with r. Case-insensitive
-creplace 'p','r' Same, case-sensitive
$1, $2 in r Insert a captured group from the pattern

-replace reads the pattern as a regex, so escape . as \. in IPs and hostnames. A literal $ in the replacement must be written $$. Without the parentheses, Get-Content ... \| Set-Content to the same file truncates it before the read finishes.

Transform characters

Lowercase a wordlist (the tr A-Z a-z move):

(Get-Content users.txt).ToLower() # uppercase to lowercase

Strip carriage returns so a script transferred from Windows runs on Linux:

(Get-Content script.sh -Raw) -replace "`r`n","`n" | Set-Content script.sh -NoNewline # CRLF to LF

Turn a comma list into one item per line:

'22,80,445' -split ',' | Set-Content ports.txt # one port per line

In a double-quoted string, PowerShell writes special characters with a backtick: `r is a carriage return, `n a line feed, `t a tab. -NoNewline stops Set-Content from adding a trailing newline.

Write output

Tee-Object writes its input to a file and to the screen at once, like Linux tee:

nxc smb $TARGET -u users.txt -p pass.txt | Tee-Object spray.txt # print and save

Append instead of overwrite:

Get-ChildItem C:\ | Tee-Object -Append listing.txt # print and append

The redirection operators and the *-Content cmdlets also write files:

systeminfo > sysinfo.txt # overwrite, like Set-Content
whoami /priv >> sysinfo.txt # append, like Add-Content
Add-Content notes.txt 'found creds in web.config' # append one line
Command Meaning
Tee-Object file Write the pipeline to file and pass it on
Tee-Object -Append Add to the file and keep its content
Set-Content / > Write, replacing the file
Add-Content / >> Append to the file
Out-File -Encoding utf8 Write with a chosen text encoding

Set-Content and > pick a default encoding that changed between PowerShell 5 and 7. For a file another tool must read back, set it with Out-File -Encoding utf8 or Set-Content -Encoding utf8.

Create files and timestamps

Create an empty file (the touch of a new file):

New-Item -ItemType File -Path notes.txt # create an empty file

Set a file’s timestamps, the anti-forensics side of touch:

$f = Get-Item shell.aspx
$f.LastWriteTime = '2023-01-15 09:30' # modification time
$f.CreationTime = '2023-01-15 09:30' # creation time
$f.LastAccessTime = '2023-01-15 09:30' # access time

Copy the timestamps of another file, so a dropped file blends in:

$ref = Get-Item C:\Windows\System32\cmd.exe
$f = Get-Item shell.aspx
$f.LastWriteTime = $ref.LastWriteTime # match a system file's time
Property Meaning
LastWriteTime When the content last changed
CreationTime The file’s creation time
LastAccessTime When the file was last read

New-Item -Force on a path that exists overwrites it with an empty file, so it is not a safe touch. To only update times, set the properties on the existing Get-Item object. Windows keeps a separate MFT change time that these properties do not reach.