制作一个.lnk,可以在新窗口中使用PowerShell打开

发布于 2025-01-28 07:16:54 字数 697 浏览 4 评论 0原文

大家好!

我希望制作PowerShell脚本,该脚本在桌面上创建.LNK快捷方式。这并不是太大了,但是当我用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()

感谢您的时间!

注意图标并在新窗口按钮中打开

Good Day everyone!

I am looking to make PowerShell script that creates a .lnk shortcut on the desktop. Which isn't too big a deal, but when I make one manually with chrome, 3 dots>more tools>create shortcut I get this option. Notice how it has the "Open in a new window" option. That is what I'm looking to accomplish. How would I go about making a .lnk file on the desktop that opens in a new window?

  • make an .lnk shortcut on desktop save it to desktop
  • .lnk file opens in new window
    -preferably grabs an icon from the browser like when you make one with chrome so I don't have to point to one.
    $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()

Thanks for your time!

Notice the Icon and Open in a New window button

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2025-02-04 07:16:54

使用以下内容来创建一个快捷键文件(.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 的图标。


为了完整性:使用目标站点的favicon 创建 url 快捷文件(.url):

请注意,此类URL快捷键文件始终:

  • 在Invocation上使用 default Web浏览器,然后默认到该浏览器自己的图标。
  • 通常,在现有的浏览器窗口中创建一个新的选项卡,而不是打开新窗口。

此答案,以编程性分配自定义图标并不直接通过wscript.shell com对象,但可以通过 plain-text处理来实现以在事实之后修改.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
"@

Use the following to create a shortcut file (.lnk) that launches Chrome with a given URL in a new window:

# 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()

Note: The above downloads and assigns the target site's specific favicon as the shortcut file's icon, you can omit the relevant code above, which will make the shortcut file show Chrome's icon.


For the sake of completeness: Creating a URL shortcut file (.url) with the target site's favicon:

Note that such URL shortcut files invariably:

  • use the default web browser on invocation, and default to that browser's own icon.
  • typically create a new tab in an existing browser window rather than opening a new window.

As explained in this answer, assigning a custom icon programmatically isn't directly supported via the WScript.Shell COM object, but can be achieved via plain-text processing to modify the .url file after the fact, as shown below.

# 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
"@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文