Powershell脚本,列出近3日打开的文件夹并打开

使用说明:

  1. 将上述代码保存为 .ps1 文件(例如 Get-RecentFolders.ps1)。

  2. 以普通用户身份运行 PowerShell,执行该脚本即可。

  3. 输出结果按最后访问时间倒序排列,包含文件夹路径和访问时间。

需要注意:这个方法依赖于 Windows 的"最近使用的项目"功能,你需要在 设置 → 个性化 → 开始 中开启"在'开始'、'跳转列表'和文件资源管理器中显示最近打开的项目",否则 Recent 文件夹可能不会记录文件夹访问。另外,该文件夹中的快捷方式有时可能指向已移动或删除的路径,脚本已做容错处理。

方式一:交互式逐个确认打开

bash 复制代码
# 列出近 3 日内打开的文件夹,并逐个询问是否打开

$recentFolderPath = [System.IO.Path]::Combine($env:APPDATA, 'Microsoft\Windows\Recent')
$cutoffDate = (Get-Date).AddDays(-3)

$recentLinks = Get-ChildItem -Path $recentFolderPath -Filter *.lnk -ErrorAction SilentlyContinue |
    Where-Object { $_.LastWriteTime -gt $cutoffDate }

$results = foreach ($link in $recentLinks) {
    try {
        $shell = New-Object -ComObject WScript.Shell
        $shortcut = $shell.CreateShortcut($link.FullName)
        $targetPath = $shortcut.TargetPath

        if ((Test-Path -Path $targetPath -PathType Container -ErrorAction SilentlyContinue)) {
            [PSCustomObject]@{
                FolderPath   = $targetPath
                LastAccessed = $link.LastWriteTime
            }
        }
    } catch {
        # 忽略无法解析的快捷方式
    }
}

$results = $results | Sort-Object LastAccessed -Descending

# 逐个确认打开
foreach ($item in $results) {
    Write-Host "文件夹: $($item.FolderPath)  (最后访问: $($item.LastAccessed))"
    $answer = Read-Host "是否打开? (Y/N)"
    if ($answer -eq 'Y' -or $answer -eq 'y') {
        Invoke-Item -Path $item.FolderPath
    }
}

方式二:直接全部打开(谨慎使用)

bash 复制代码
# 列出近 3 日内打开的文件夹,并全部打开

$recentFolderPath = [System.IO.Path]::Combine($env:APPDATA, 'Microsoft\Windows\Recent')
$cutoffDate = (Get-Date).AddDays(-3)

$recentLinks = Get-ChildItem -Path $recentFolderPath -Filter *.lnk -ErrorAction SilentlyContinue |
    Where-Object { $_.LastWriteTime -gt $cutoffDate }

$results = foreach ($link in $recentLinks) {
    try {
        $shell = New-Object -ComObject WScript.Shell
        $shortcut = $shell.CreateShortcut($link.FullName)
        $targetPath = $shortcut.TargetPath

        if ((Test-Path -Path $targetPath -PathType Container -ErrorAction SilentlyContinue)) {
            [PSCustomObject]@{
                FolderPath   = $targetPath
                LastAccessed = $link.LastWriteTime
            }
        }
    } catch {
        # 忽略无法解析的快捷方式
    }
}

$results = $results | Sort-Object LastAccessed -Descending

# 全部打开
foreach ($item in $results) {
    Write-Host "打开: $($item.FolderPath)"
    Invoke-Item -Path $item.FolderPath
}
相关推荐
Star(up up up)16 小时前
Redis Windows 安装教程:使用Windows 的 Redis 压缩包
redis·笔记·学习·程序人生·云原生·学习方法·改行学it
秋天的怀念_David6 天前
算法一千零一夜 · 第二十八夜 注册
java·程序人生·算法
ShuangNaoPai8 天前
饱和品类的心智词生成:从“新米”认知到“五新米”的判定路由
人工智能·程序人生·产品·思维·品牌营销
浮尘笔记10 天前
JetBrains差异设置被自动勾选的排查与根治实录
经验分享·程序人生·编辑器·intellij idea·phpstorm·webstorm
yuezhilangniao12 天前
win10中文 以及 PowerShell 开发日常
windows·powershell
秋天的怀念_David13 天前
算法一千零一夜 · 第二十三夜 电力民生写成的曲线
java·程序人生·算法
郝学胜-神的一滴13 天前
AI 编程智能体 22:解析器赋能与实战落地全指南
人工智能·程序人生·游戏·unity·游戏引擎
Star(up up up)13 天前
Docker系列---Docker容器基本命令(新手小白保姆级实操教程)
运维·学习·程序人生·青少年编程·docker·容器·dubbo
Star(up up up)13 天前
Docker系列---Dockerfile镜像文件
人工智能·经验分享·学习·程序人生·docker·容器·github