实用脚本集合#

以下 PowerShell 脚本可直接在 Windows 环境中使用。

1. 每日备份脚本#

# backup-claude.ps1
$date = Get-Date -Format "yyyy-MM-dd"
$backupDir = "D:\Claude-Backups\$date"

Write-Host "备份 $date 的所有对话..." -ForegroundColor Cyan
claude-extract --search "" --search-date-from $date --format markdown --output $backupDir

if (Test-Path $backupDir) {
    $files = Get-ChildItem -Path $backupDir -Recurse -File
    $totalSize = ($files | Measure-Object -Property Length -Sum).Sum / 1KB
    Write-Host "备份完成: $($files.Count) 个文件, $([math]::Round($totalSize, 2)) KB" -ForegroundColor Green
}

2. 快速导出脚本#

# export-session.ps1
param(
    [Parameter(Mandatory)]
    [int]$Number,
    [string]$Format = "markdown",
    [string]$Output = "./exports"
)

$dir = "$Output/session-$Number-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
claude-extract --extract $Number --format $Format --output $dir

if (Test-Path $dir) {
    Write-Host "导出完成: $dir" -ForegroundColor Green
    if ($Format -eq "html") { start "$dir\*.html" }
}

3. 全量 HTML 归档脚本#

# full-archive.ps1
param(
    [string]$Output = "D:\Claude-Full-Archive\$(Get-Date -Format 'yyyy-MM-dd')"
)

Write-Host "=== 全量归档 ===" -ForegroundColor Cyan

# 工具 1: claude-code-transcripts
uvx claude-code-transcripts all -o "$Output\transcripts-html" -q

# 工具 2: ai-code-sessions
uvx ai-code-sessions all -o "$Output\sessions-html" -q

# 工具 3: claude-conversation-extractor
claude-extract --all --format markdown --output "$Output\extractor-md"

Write-Host "全量归档完成: $Output" -ForegroundColor Green

常用单行命令速查#

# 查看最近 10 个会话
claude-extract --list --limit 10

# 导出最新会话为 HTML 并打开
claude-extract --extract 1 --format html --output ./tmp && start ./tmp/*.html

# 搜索关键词
claude-search "PowerShell"

# 导出所有会话为 HTML(transcripts 工具)
uvx claude-code-transcripts all -o ./archive --open

# 导出所有会话为 HTML(sessions 工具)
uvx ai-code-sessions all -o ./archive --open