使用说明:
-
将上述代码保存为
.ps1文件(例如Get-RecentFolders.ps1)。 -
以普通用户身份运行 PowerShell,执行该脚本即可。
-
输出结果按最后访问时间倒序排列,包含文件夹路径和访问时间。
需要注意:这个方法依赖于 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
}
