I want to create a shortcut file to a PDF document with arguments, so that when I double click it on Explorer, it will open the pdf file in my default PDF viewer at the page specified.
I can create such a shortcut in Explorer, by right clicking the PDF file then:
PDFXEdit /a page=87 "C:\temp\sample.pdf"
I would like a PowerShell means of doing the above, I tried:
set-shorcut -Path 'c:\temp\sample.pdf' -Arguments 'PDFXEdit /a page=87' | get-shortcut
WindowStyle : 1
Path : C:\temp\sample.lnk
IconPath : ,0
Hotkey :
Arguments : PDFXEdit /a page=87
Target : C:\temp\sample.pdf
The shortcut does get created but when I look at the target field for the shortcuts file's properties dialog, its value is:
"C:\temp\sample.pdf" PDFXEdit /a page=87
and executing it does open the file with the default pdf programme but not at page 87.
It seems that the expected value is PDFXEdit /a page=87 "C:\temp\sample.pdf"
yet I cant seem to do this in PowerShell.
Is it not possible for .lnk
files, to just attempt to pass a file path and some arbitrary argument to the current 'open with...' pdf programme??
Any help would be greatly appreciated!
The code for set-shortcut
is:
function Set-Shortcut{
param(
[Parameter(ValueFromPipelineByPropertyName)]
[String]$Path = $pwd,
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[String]
$Target,
[Parameter(ValueFromPipelineByPropertyName)]
[String]
$Arguments,
[Parameter(ValueFromPipelineByPropertyName)]
[String]
$IconPath
)
process{
if (-not(get-item -path $path).PSIsContainer){Write-Error 'Only directories allowed as destination Path' -ErrorAction Stop}
$path = join-path -path $path -ChildPath ((get-item -path $target).BaseName + '.lnk')
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($Path)
$Shortcut.TargetPath = $Target
Switch ($PSBoundParameters.keys){
'Arguments' {$Shortcut.Arguments = $Arguments}
'IconPath' {$Shortcut.IconLocation = $IconPath}
}
$Shortcut.Save()
return (Get-Item $Path)
}
}
When you manually create the shortcut you then edit it to completely change what the shortcut points to. Specifically you change it to point at the PDFXEdit application, with arguments including the path to the file and page number. So when you create the shortcut in PowerShell you would still want to replicate those changes.
set-shorcut -Path PDFXEdit -Arguments '/a page=87 "c:\temp\sample.pdf"' | get-shortcut