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
}
相关推荐
HugoStudio_SWAN1 天前
洛谷 B4575 [GESP202609 二级] 直角三角形——临场定义理解与边界质疑
c++·学习·程序人生·算法
JackSparrow4142 天前
和AI一起将全部CSDN博文迁移到个人博客站
人工智能·程序人生·ai·github·cloudflare·astro·静态博客
谷哥的小弟2 天前
基于PowerShell的Chrome无痕访问
chrome·powershell·网络环境·无痕访问
特立独行的猫a3 天前
八零后熟悉的世界,正在一点点消失
程序人生·生活
郝学胜-神的一滴3 天前
AI 编程智能体 03:拆解当下主流智能体能力
开发语言·c++·人工智能·python·程序人生·游戏
Single4 天前
无影脚通行|人类:星际物种的地球预演
程序人生·其他·人机交互·产品经理·业界资讯
郝学胜-神的一滴4 天前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建
郝学胜-神的一滴4 天前
AI 编程智能体 02:AI智能体到底是什么
开发语言·人工智能·python·程序人生·pycharm
可爱系程序猿4 天前
键鼠都正常、只有手柄映射才提示 dinput8.dll?DirectInput 接口和游戏目录怎么对齐
程序人生·游戏·电脑