实用脚本集合#

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

1. 每日备份脚本#

backup-claude.ps1 — 备份当天所有 Claude 对话。

# 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
    Write-Host "位置: $backupDir" -ForegroundColor Yellow
} else {
    Write-Host "今天没有新的对话" -ForegroundColor Gray
}

使用方式:

.\backup-claude.ps1

2. 快速导出脚本#

export-session.ps1 — 按编号导出指定会话,支持三种格式。

# 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" }
}

使用方式:

# 导出编号 47 的会话为 Markdown
.\export-session.ps1 -Number 47

# 导出为 HTML 并自动打开
.\export-session.ps1 -Number 47 -Format html

# 导出为 JSON 到指定目录
.\export-session.ps1 -Number 47 -Format json -Output "D:\exports"

3. 按主题归档脚本#

archive-by-topic.ps1 — 按关键词分类导出对话。

# archive-by-topic.ps1
param(
    [string]$Output = "./topic-archives"
)

$topics = @{
    "Python"     = "Python"
    "PowerShell" = "PowerShell"
    "MCP"        = "MCP"
    "Git"        = "Git"
    "Docker"     = "Docker"
}

foreach ($name in $topics.Keys) {
    $keyword = $topics[$name]
    $dir = "$Output/$name"
    Write-Host "导出主题: $name ..." -ForegroundColor Yellow
    claude-extract --search $keyword --format markdown --output $dir 2>$null
}

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

使用方式:

.\archive-by-topic.ps1
.\archive-by-topic.ps1 -Output "D:\Claude-Archives"

4. 月度报告脚本#

monthly-report.ps1 — 生成指定月份的 HTML 报告。

# monthly-report.ps1
param(
    [int]$Year = (Get-Date).Year,
    [int]$Month = (Get-Date).Month
)

$start = "{0:yyyy-MM-dd}" -f (Get-Date -Year $Year -Month $Month -Day 1)
$end = "{0:yyyy-MM-dd}" -f (Get-Date -Year $Year -Month $Month -Day 1).AddMonths(1).AddDays(-1)
$dir = "./reports/$Year-$('{0:D2}' -f $Month)"

Write-Host "生成 $start$end 的报告..." -ForegroundColor Cyan
claude-extract --search "" --search-date-from $start --search-date-to $end --format html --output $dir

if (Test-Path $dir) {
    Write-Host "报告位置: $dir" -ForegroundColor Green
    start $dir
}

使用方式:

# 当月报告
.\monthly-report.ps1

# 指定月份
.\monthly-report.ps1 -Year 2026 -Month 1

5. 全量 HTML 归档脚本#

full-archive.ps1 — 使用三款工具分别生成归档。

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

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

# 工具 1: claude-code-transcripts (HTML 归档)
Write-Host "1/3 claude-code-transcripts..." -ForegroundColor Yellow
uvx claude-code-transcripts all -o "$Output\transcripts-html" -q

# 工具 2: ai-code-sessions (HTML 归档)
Write-Host "2/3 ai-code-sessions..." -ForegroundColor Yellow
uvx ai-code-sessions all -o "$Output\sessions-html" -q

# 工具 3: claude-conversation-extractor (Markdown + JSON)
Write-Host "3/3 claude-conversation-extractor..." -ForegroundColor Yellow
claude-extract --all --format markdown --output "$Output\extractor-md"
claude-extract --all --format json --output "$Output\extractor-json"

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

6. 查找会话编号脚本#

find-session.ps1 — 通过关键词或会话 ID 查找编号。

# find-session.ps1
param(
    [Parameter(Mandatory)]
    [string]$Keyword,
    [int]$Limit = 100
)

Write-Host "搜索: $Keyword" -ForegroundColor Cyan
claude-extract --list --limit $Limit | Select-String $Keyword -Context 2

使用方式:

# 按关键词查找
.\find-session.ps1 -Keyword "PowerShell"

# 按会话 ID 查找
.\find-session.ps1 -Keyword "bdff3c13"

# 按日期查找
.\find-session.ps1 -Keyword "2026-02-14"

常用单行命令速查#

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

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

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

# 导出最近 5 个会话
claude-extract --recent 5 --output ./recent

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

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