0

I am trying to create a shortcut to the network printer.

Here is the PowerShell script:

$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 /y /in /q /n \\192.168.1.205\printer1"
$s.IconLocation = "imageres.dll,76"
$s.Save() 

When I run the script, it generates the shortcut however the "Target" part of the shortcut appears with Forward slash changed to backward slash on all switches, one of the back slash removed from the IP Address and quotes added to the start & end of the whole line)

"C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry \y \in \q \n \192.168.1.205\printer1"

What changes I need to do on the script so that it will generate the correct Target like below:

C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /y /in /q /n "\\192.168.1.205\printer1"

Any help would be much appreciated, thank you.

bickyz
  • 37
  • 2
  • 9

1 Answers1

2

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

Shortcut Properties

Theo
  • 57,719
  • 8
  • 24
  • 41
  • I have tried this however it generate shortcut with Target Type & Target as "This PC" and Target Location as "This PC" – bickyz Aug 19 '19 at 12:59
  • @bickyz No it doesn't. If I run this, the Target is `C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /y /in /q /n \\192.168.1.205\printer1`. The TargetType is `Executable`. The TargetPath is `System32`. The Location of the shortcut itself is of course your computer/your desktop because that is what we did for the ShortcutPath. I've added a screenshot showing the properties of the created shortcut. (It's from my Dutch machine but you'll figure it out). Just copy the code EXACTLY as above. – Theo Aug 19 '19 at 14:33
  • I was running from PowerShell ISE earlier. Saved as a PS1 and ran from PowerShell which generated shortcut with correct details. thank you. – bickyz Aug 19 '19 at 18:30