大家好!
我正在寻找制作在桌面上创建 .lnk 快捷方式的 PowerShell 脚本。这没什么大不了的,但是当我用 chrome 手动制作一个时,3 个点>更多工具>创建快捷方式我得到了这个选项。请注意它如何具有“在新窗口中打开”选项。这就是我想要实现的目标。我该如何在桌面上创建一个在新窗口中打开的 .lnk 文件?
- 在桌面上创建一个 .lnk 快捷方式并将其保存到桌面
- .lnk 文件在新窗口中打开 - 最好从浏览器中获取一个图标,就像使用 Chrome 制作图标一样,这样我就不必指向一个图标。
$Shell = New-Object -ComObject ("WScript.Shell")
$Favorite = $Shell.CreateShortcut($env:USERPROFILE + "\Desktop\Google.lnk")
$Favorite.TargetPath = "http://google.com";
$Favorite.IconLocation = "Picturelocation"
$Favorite.Arguments
$Favorite.Save()
感谢您的宝贵时间!
最佳答案
使用以下命令创建一个快捷方式文件 (.lnk
),用于在新窗口中使用给定 URL 启动 Chrome:
# Define the target URL
$url = 'https://google.com'
# Derive a friendly site name from it, to serve as the name
# of the shortcut file and the downloaded favicon.
# Adjust as needed, e.g. $friendlySiteName = 'Google'
$friendlySiteName = $url -replace '^https?://(?:www\.)?'
# Determine the full path of the shortcut file; adjust as needed.
# Note the required ".lnk" extension
$shortcutPath = "$HOME\Desktop\$friendlySiteName.lnk"
# Determine the full path of the chrome.exe executable, via the registry.
# Note: This is only necessary because chrome.exe is *not* in one
# of the directories listed in $env:PATH.
$chromeExePath = Get-ItemPropertyValue -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' '(default)'
# Download the favicon:
# Create a designated local directory for storing favicons...
$targetDir = New-Item -Type Directory -Force "$HOME\favicons"
# ... and download the target site's favicon into it.
$favIconPath = Join-Path $targetDir.FullName ($friendlySiteName + '.ico')
& {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -OutFile $favIconPath "$url/favicon.ico"
}
# Create the shortcut file, set its properties, and save it.
$shell = New-Object -ComObject WScript.Shell
$favorite = $shell.CreateShortcut($shortcutPath)
# Tell the shortcut to launch Chrome...
$favorite.TargetPath = $chromeExePath
# ... with the following arguments; -new-window ensures that the
# specified URL is opened in a new window.
$favorite.Arguments = "-new-window $url"
# ... and assign the icon.
$favorite.IconLocation = $favIconPath
$favorite.Save()
注意:上面下载并分配目标站点的具体favicon作为快捷方式文件的图标,您可以省略上面的相关代码,这将使快捷方式文件显示Chrome的图标。
为了完整起见:使用目标网站的图标创建 URL 快捷方式文件 (.url
):
请注意,此类 URL 快捷方式文件总是:
- 在调用时使用默认网络浏览器,并默认使用该浏览器自己的图标。
- 通常在现有浏览器窗口中创建新选项卡,而不是打开新窗口。
如 this answer 中所述,通过 WScript.Shell
COM 对象不直接支持以编程方式分配自定义图标,但可以通过纯文本处理来修改 .url 来实现
事后文件,如下所示。
# Define the target URL.
$url = 'https://google.com'
# Derive a friendly representation from it, to serve as the name of the shortcut file
# and the downloaded favicon.
# Adjust as needed; e.g. $friendlySiteName = 'Google'
$friendlySiteName = $url -replace '^https?://(?:www\.)?'
# Determine the full path of the shortcut file; adjust as needed.
# Note the required ".url" extension.
$urlShortcutPath = "$HOME\Desktop\$friendlySiteName.url"
# Download the favicon:
# Create a designated local directory for storing favicons...
$targetDir = New-Item -Type Directory -Force "$HOME\favicons"
# ... and download the target site's favicon into it.
$favIconPath = Join-Path $targetDir.FullName ($friendlySiteName + '.ico')
& {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -OutFile $favIconPath "$url/favicon.ico"
}
# Create the URL shortcut file, set its properties, and save it.
$shell = New-Object -ComObject WScript.Shell
$urlShortcut = $shell.CreateShortcut($urlShortcutPath)
# Tell the URL shortcut what URL to launch.
# !! No other properties are directly supported for URL shortcut files.
# !! Plain-text processing below compensates for that.
$urlShortcut.TargetPath = $url
$urlShortcut.Save()
# Now use plain-text processing to add the icon location.
Add-Content -LiteralPath $urlShortcut.FullName -Value @"
IconIndex=0
HotKey=0
IconFile=$favIconPath
"@
关于powershell - 使用 powershell 制作在新窗口中打开的 .lnk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72193704/