2

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
Will
  • 21
  • 2
  • If you place this code `$objShell = New-Object -ComObject WScript.Shell;Get-ChildItem -Path "$PSScriptRoot\*.lnk" -File | ForEach-Object {$objShell.CreateShortcut($_.FullName)} | Format-List -Property *` in a .ps1 file, copy a .lnk to the same folder as the .ps1 file, and run the .ps1 file, it should give you the properties of the .lnk file. You should be able to use this to backwards engineer a .lnk file to get the values you need to set. Check this answer for example getting the icon info: https://stackoverflow.com/a/72161775/4190564 – Darin May 11 '22 at 02:08

1 Answers1

3

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
"@
mklement0
  • 382,024
  • 64
  • 607
  • 775