Identify and safely replace known literal text in selected UTF-8 website files.

Sheet: 13 of 25 | Platform: Windows 10/11 | Shell: PowerShell 7.x and Windows PowerShell 5.1 (unless marked otherwise) | Editorial review: November 24, 2026

PowerShell cheat sheet index

Examples use the MGU project root and illustrative staging, backup, and download paths. Replace them with existing, approved paths as needed. Unless explicitly marked as a continuation, examples are independent. Read commands before running them; commands that write or modify files can change existing data.

Quick commands

1. Find affected HTML files without modifying them

$old = '/component/old.css'
Get-ChildItem -LiteralPath 'C:\MGU' -Recurse -File -Filter '*.html' |
    Select-String -Pattern $old -SimpleMatch -List | Select-Object Path

2. Preview a literal replacement in memory

$path = 'C:\MGU-Staging\article.html'
$text = Get-Content -LiteralPath $path -Raw -Encoding UTF8
$text.Replace('/component/old.css','/component/lib.css')

3. Preview changed-file inventory (known UTF-8 files)

Get-ChildItem -LiteralPath 'C:\MGU-Staging' -Recurse -File -Filter '*.html' |
    ForEach-Object {
        $text = Get-Content -LiteralPath $_.FullName -Raw -Encoding UTF8 -ErrorAction Stop
        if ($null -ne $text -and $text.Contains('/component/old.css')) { $_.FullName }
    }

4. Replace text in one backed-up staging file (PowerShell 7)

$path = 'C:\MGU-Staging\article.html'
$old = '/component/old.css'; $new = '/component/lib.css'
$text = Get-Content -LiteralPath $path -Raw -Encoding UTF8 -ErrorAction Stop
if ($null -ne $text -and $text.Contains($old)) {
    $backup = $path + '.bak'
    if (Test-Path -LiteralPath $backup) { throw 'Backup already exists.' }
    Copy-Item -LiteralPath $path -Destination $backup -ErrorAction Stop
    Set-Content -LiteralPath $path -Value $text.Replace($old,$new) -NoNewline -Encoding utf8NoBOM -ErrorAction Stop
}

5. Verify whether the old string remains

Select-String -LiteralPath 'C:\MGU-Staging\article.html' -Pattern '/component/old.css' -SimpleMatch

Operational notes

String.Replace performs case-sensitive literal substitution; -replace is regex-based and case-insensitive by default. Confirm input encoding and preserve backups. The write example normalizes to UTF-8 without BOM; it is not byte-preserving for files originally using another encoding.

Official documentation