【问题标题】:Copy shortcut favorites from windows 7 windows explorer to a text file将快捷方式收藏夹从 Windows 7 Windows 资源管理器复制到文本文件
【发布时间】:2019-01-04 21:35:35
【问题描述】:

我有一个 powershell 脚本,可以将快捷方式收藏夹从 Windows 7 Windows 资源管理器复制到文本文件。

这是一个有效的脚本:

$ShortcutsFile = 'H:\_ProfilBackup\Genveje.txt'

# Copy Shortcuts to file

if (!(Test-Path $ShortcutsFile)) { New-Item -Path $ShortcutsFile -ItemType File -Force | Out-Null }

$Shortcuts = Get-ChildItem -Recurse "$HOME\Links" -Include *.lnk

$Shell = New-Object -ComObject WScript.Shell
foreach ($Shortcut in $Shortcuts){
$Properties = @{
    Shortcuttarget = $shortcut.Target
    Target = $Shell.CreateShortcut($Shortcut).targetpath | Out-File $ShortcutsFile -Append
}#$Properties
New-Object PSObject -Property $Properties
}#foreach
[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null

我的问题是:

我只能在执行以下操作时获取文件的所有链接路径属性:

Out-File from the PSObject -Property $Properties = @{ 

Target = $Shell.CreateShortcut($Shortcut).targetpath | Out-File $ShortcutsFile -Append 

有没有人知道如何将所有链接路径属性获取到一个变量?

$shortcut.Target 只有最后一个文件链接

有谁知道另一种方法吗?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我不清楚你期望什么输出。
    此脚本将属性输出到屏幕(以列表格式)
    和/或 .csv 文件

    $ShortcutsFile = 'H:\_ProfilBackup\Genveje.txt'
    
    $ShortcutPath = "$HOME\Links" 
    $Shell = New-Object -ComObject WScript.Shell
    
    $ShortCuts = foreach ($Shortcut in (Get-ChildItem $ShortcutPath -Recurse -Include *.lnk)){
         $Shell.CreateShortcut($Shortcut.FullName)
    }
    
    $shortcuts 
    #$shortcuts | Export-Csv $ShortcutsFile -NoTypeInformation
    

    示例屏幕输出:

    FullName         : C:\Users\LotPings\Links\Desktop.lnk
    Arguments        :
    Description      :
    Hotkey           :
    IconLocation     : ,0
    RelativePath     :
    TargetPath       : C:\Users\LotPings\Desktop
    WindowStyle      : 1
    WorkingDirectory :
    
    FullName         : C:\Users\LotPings\Links\Downloads.lnk
    Arguments        :
    Description      :
    Hotkey           :
    IconLocation     : ,0
    RelativePath     :
    TargetPath       : C:\Users\LotPings\Downloads
    WindowStyle      : 1
    WorkingDirectory :
    

    样本 csv:

    "FullName","Arguments","Description","Hotkey","IconLocation","RelativePath","TargetPath","WindowStyle","WorkingDirectory"
    "C:\Users\LotPings\Links\Desktop.lnk","","","",",0",,"C:\Users\LotPings\Desktop","1",""
    "C:\Users\LotPings\Links\Downloads.lnk","","","",",0",,"C:\Users\LotPings\Downloads","1",""
    

    【讨论】:

    • 非常感谢 LotPings 认为这正是我所需要的。不喜欢我的脚本的输出方式。我会尽快尝试。
    【解决方案2】:

    使用这个

    https://github.com/gangstanthony/PowerShell/blob/master/Get-Shortcut.ps1

    $shortcutinfo = Get-ChildItem -path "$HOME\Links" -Include '*.lnk' -Recurse | foreach-object -process {get-shortcut -path $_.fullname}
    
    $shortcutinfo | export-csv -path H:\ProfilBackup\Genveje.csv -notypeinformation
    

    【讨论】:

    • 谢谢安东尼。我不确定您所说的 get-shortcut 是什么意思?
    • 他建议您使用他在答案中包含的链接中提供的功能 Get-Shortcut。
    • 好的,谢谢您的信息。我真的很想找到一种不使用函数的方法。必须有一种方法可以将所有链接路径属性从上述脚本中获取到变量
    • @Joe,我很高兴您找到了解决方案,但没有任何理由,似乎通过避免函数来限制您的脚本似乎并不是一个优势。如果您单击该链接,您会发现没有任何恶意。 powershell 中的函数只是工具箱中的一个工具,例如 if 语句、for 循环、哈希表,甚至变量。我希望您最终能够更加熟悉函数以及它们如何提供帮助。保重。
    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多