//nbkelley /homelab

PowerShell Local PC Scripting Patterns

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):

PowerShell Local PC Scripting Patterns

What Was Established#

  • Standardized PowerShell commands for local file operations and MSI package management.
  • A streamlined, linear pattern for automating software installation, execution, and uninstallation on local Windows hosts without over-engineering modular functions.

Key Decisions#

  • Prefer Get-ChildItem with wildcards (C:\Users\*\Downloads\*.msi) for rapid file discovery over manual profile loops.
  • Use Start-Process with msiexec.exe for silent MSI installs (/qn) to keep scripts concise for linear workflows.
  • Simplified error handling and removed verbose logging for basic administrative tasks, reserving complex wrappers for larger automation frameworks.

Current Configuration / Patterns#

File Listing & Discovery#

# List files recursively, sorted newest first
Get-ChildItem -Path "C:\Path" -File -Recurse | Sort-Object LastWriteTime -Descending

# Find first matching file across user profiles
Get-ChildItem "C:\Users\*\Downloads\CleanUpTool.msi" -ErrorAction SilentlyContinue | Select-Object -First 1

File Copying#

# Copy single file, force overwrite
Copy-Item -Path "Source\File.ext" -Destination "Target\" -Force

# Copy entire directory tree
Copy-Item -Path "C:\Source\*" -Destination "D:\Destination\" -Recurse

MSI Install / Run / Uninstall Pattern#

#Requires -RunAsAdministrator

# 1. Find and Install
$cleanupPath = Get-ChildItem "C:\Users\*\Downloads\CleanUpTool.msi" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($cleanupPath) {
    Start-Process "msiexec.exe" -ArgumentList "/i `"$($cleanupPath.FullName)`" /qn" -Wait

    # 2. Run (locate executable in Program Files)
    $exePath = Get-ChildItem "C:\Program Files\", "C:\Program Files (x86)\" -Recurse -Filter "*CleanUpTool*.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
    if ($exePath) { Start-Process $exePath.FullName -Wait }

    # 3. Uninstall
    $product = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -like "*CleanUpTool*" }
    if ($product) { $product.Uninstall() }
}

# 4. Install Next Package
$foxitPath = Join-Path $PSScriptRoot "Foxit.msi"
if (Test-Path $foxitPath) {
    Start-Process "msiexec.exe" -ArgumentList "/i `"$foxitPath`" /qn" -Wait
}

Historical Notes#

  • Conversation dated 2025-06-11. The simplified linear script approach was explicitly chosen over a modular function-based approach to reduce overhead for routine local PC tasks.
  • Get-WmiObject -Class Win32_Product is a legacy enumeration method. While functional for basic scripts, it is known to trigger package repairs and is slow on large systems. Consider migrating to Get-CimInstance -Class Win32_Product or querying HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall for production use.

Open Questions#

  • Does the local PC environment still rely on Get-WmiObject for software inventory, or has it migrated to Get-CimInstance / registry queries?
  • Are there standardized locations for MSI installers beyond user Downloads and script root for future automation?
  • wiki/scripts/powershell-cleanup-patterns.md
  • wiki/scripts/bulk-folder-rename-script.md
  • wiki/administration/powershell-administration-commands.md

Sources#

  • ingested/chats/056-List Files in Folder Using PowerShell.md
  • DeepSeek conversation: “List Files in Folder Using PowerShell” (2025-06-11)