//nbkelley /homelab

PowerShell Local PC Scripting Patterns#

What Was Established#

A collection of PowerShell scripting patterns for local PC automation tasks, including folder renaming from CSV data, and basic system administration.

Key Decisions#

  • CSV-driven automation: Use Import-Csv with Rename-Item for bulk folder renaming driven by spreadsheet data.
  • Path validation: Always Test-Path before operating to avoid errors on missing sources or existing destinations.
  • Error handling: Wrap rename operations in conditional checks; log warnings rather than failing silently.

Folder Renaming from CSV#

CSV format (folder_renaming.csv):

CurrentName,NewName
"Old Folder 1","New Folder 1"
"Old Folder 2","New Folder 2"

PowerShell script:

$csvFile = "folder_renaming.csv"
$basePath = "C:\Your\Base\Path\"

$folders = Import-Csv $csvFile

foreach ($folder in $folders) {
    $currentPath = Join-Path $basePath $folder.CurrentName
    $newPath = Join-Path $basePath $folder.NewName

    if (Test-Path $currentPath -PathType Container) {
        if (-not (Test-Path $newPath)) {
            Rename-Item -Path $currentPath -NewName $folder.NewName
            Write-Host "Renamed: '$($folder.CurrentName)' -> '$($folder.NewName)'"
        } else {
            Write-Warning "Cannot rename: '$($folder.NewName)' already exists"
        }
    } else {
        Write-Warning "Folder not found: '$($folder.CurrentName)'"
    }
}

Cross-Platform Equivalents#

Python variant (cross-platform, uses pandas):

import pandas as pd, os
df = pd.read_csv("folder_renaming.csv")
for _, row in df.iterrows():
    os.rename(row['CurrentName'], row['NewName'])

Bash variant (Linux):

while IFS=, read -r old new; do
    [ -d "$old" ] && [ ! -e "$new" ] && mv "$old" "$new"
done < <(tail -n +2 folder_renaming.csv)

Historical Notes#

  • Conversation date: 2025-11-13.
  • Scripts assume standard local file system; network/UNC paths may require additional error handling.
  • No homelab infrastructure involvement — local PC automation only.
  • None yet.

Sources#

  • ingested/chats/107-I have a public folder set up fo.md
  • ingested/chats/099-Automate Folder Renaming with Excel Data.md
  • DeepSeek conversation: “Automate Folder Renaming with Excel Data” (chat 099)