I am trying to create a shortcut to the network printer.
Here is the PowerShell script:
$DesktopPath = [Environment]::GetFolderPath(\"Desktop\")
$cre
I can't find the documentation for it (good old Microsoft) but essentially you need to set the value of Arguments
:
$DesktopPath = [Environment]::GetFolderPath("Desktop")
$create_shortcut = (New-Object -ComObject WScript.Shell).CreateShortcut
$s = $create_shortcut.invoke("$DesktopPath\ConnectPrinter.lnk")
$s.TargetPath = "C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry"
# add this
$s.Arguments = "/y /in /q /n ""\\192.168.1.205\printer1"""
$s.IconLocation = "imageres.dll,76"
$s.Save()
I'm not sure whether printui.dll,PrintUIEntry
should be part of the TargetPath
or Arguments
For reusability, I wrote a helper function some time ago to create new shortcut files.
As a bonus, it also allows you to set the 'Run as Administrator' checkmark (not needed for this question though)
function New-Shortcut {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$TargetPath, # the path to the executable
# the rest is all optional
[string]$ShortcutPath = (Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'New Shortcut.lnk'),
[string[]]$Arguments = $null, # a string or string array holding the optional arguments.
[string[]]$HotKey = $null, # a string like "CTRL+SHIFT+F" or an array like 'CTRL','SHIFT','F'
[string]$WorkingDirectory = $null,
[string]$Description = $null,
[string]$IconLocation = $null, # a string like "notepad.exe, 0"
[ValidateSet('Default','Maximized','Minimized')]
[string]$WindowStyle = 'Default',
[switch]$RunAsAdmin
)
switch ($WindowStyle) {
'Default' { $style = 1; break }
'Maximized' { $style = 3; break }
'Minimized' { $style = 7 }
}
$WshShell = New-Object -ComObject WScript.Shell
# create a new shortcut
$shortcut = $WshShell.CreateShortcut($ShortcutPath)
$shortcut.TargetPath = $TargetPath
$shortcut.WindowStyle = $style
if ($Arguments) { $shortcut.Arguments = $Arguments -join ' ' }
if ($HotKey) { $shortcut.Hotkey = ($HotKey -join '+').ToUpperInvariant() }
if ($IconLocation) { $shortcut.IconLocation = $IconLocation }
if ($Description) { $shortcut.Description = $Description }
if ($WorkingDirectory) { $shortcut.WorkingDirectory = $WorkingDirectory }
# save the link file
$shortcut.Save()
if ($RunAsAdmin) {
# read the shortcut file we have just created as [byte[]]
[byte[]]$bytes = [System.IO.File]::ReadAllBytes($ShortcutPath)
# $bytes[21] = 0x22 # set byte no. 21 to ASCII value 34
$bytes[21] = $bytes[21] -bor 0x20 #s et byte 21 bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes($ShortcutPath, $bytes)
}
# clean up the COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
You can use it like this
$props = @{
'ShortcutPath' = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'ConnectPrinter.lnk'
'TargetPath' = 'C:\Windows\System32\rundll32.exe'
'Arguments' = 'printui.dll,PrintUIEntry', '/y', '/in', '/q', '/n', '\\192.168.1.205\printer1'
'IconLocation' = 'imageres.dll,76'
'Description' = 'Connect to Printer1'
}
New-Shortcut @props