答案1
这个 PowerShell 函数可以解决问题。
将其保存为任意位置Create-ShortcutForEachFile.ps1
将其加载到 PowerShell 会话中,如下所示:. C:\somewhere\Create-ShortcutForEachFile.ps1
然后像这样使用它:Create-ShortcutForEachFile -Source C:\foo -Destination C:\bar -Recurse
function Create-ShortcutForEachFile {
Param(
[ValidateNotNullOrEmpty()][string]$Source,
[ValidateNotNullOrEmpty()][string]$Destination,
[switch]$Recurse
)
# set recurse if present
if ($Recurse.IsPresent) { $splat = @{ Recurse = $true } }
# Getting all the source files and source folder
$gci = gci $Source @splat
$Files = $gci | ? { !$_.PSisContainer }
$Folders = $gci | ? { $_.PsisContainer }
# Creating all the folders
if (!(Test-Path $Destination)) { mkdir $Destination -ea SilentlyContinue > $null }
$Folders | % {
$Target = $_.FullName -replace [regex]::escape($Source), $Destination
mkdir $Target -ea SilentlyContinue > $null
}
# Creating Wscript object
$WshShell = New-Object -comObject WScript.Shell
# Creating all the Links
$Files | % {
$InkName = "{0}.lnk" -f $_.BaseName
$Target = ($_.DirectoryName -replace [regex]::escape($Source), $Destination) + "\" + $InkName
$Shortcut = $WshShell.CreateShortcut($Target)
$Shortcut.TargetPath = $_.FullName
$Shortcut.Save()
}
}