creating quoted path for shortcut with arguments in powershell
Asked Answered
A

2

7

I have the following powershell code calling WSHShell which will create a shortcut in the start menu for Win7/8 but am unable to figure out how to get powershell to pass the quotes needed around the UNC path prior to the arguments in the target line.

What I want: "\\UNCPATH1\Directory\application.exe" argumentA ArgumentB

What I get: \\UNCPATH1\Directory\application.exe argumentA ArgumentB

Code as presently used:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = "\\UNCPATH1\Directory\application.exe"
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "\\UNCPATH1\Directory"
$Shortcut.Save()

Edit with Code examples... thanks to TheMadTechnician and Speerian who both had working examples. Windows is stripping quoted paths in the target field from shortcuts that do not have a space in the application UNC path. Both code examples work on paths with spaces.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = "`"\\UNCPATH1\Directory1\application.exe`""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory1"'
$Shortcut.Save()

or

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = """\\UNCPATH1\Directory 1\application.exe"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "\\UNCPATH1\Directory 1"
$Shortcut.Save()

On the second example note the space in UNC path and the removal of the single quotes from workingdirectory in the shortcut attributes. (windows will automatically add here)

Antoniettaantonin answered 4/8, 2015 at 17:7 Comment(1)
I've managed to have the opposite problem. I don't include quotes when setting TargetPath, but they're added to the resulting shortcut.Edenedens
F
10

Place your quoted string within other quotes, so "\\UNCPATH1\Directory\application.exe" will become '"\\UNCPATH1\Directory\application.exe"'.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = '"\\UNCPATH1\Directory\application.exe"'
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory"'
$Shortcut.Save()

Edit: ...and I was wrong. This does work for the WorkingDirectory property but not the TargetPath property. What does work is to triple-quote your string instead. So, that leads us to this:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$([environment]::GetFolderPath("Desktop"))\mrincredible.lnk")
$Shortcut.TargetPath = """\\UNCPATH1\Directory 1\application.exe"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory"'
$Shortcut.Save()

Works fine on Windows 8.1 at the very least.

Fastigiate answered 4/8, 2015 at 17:12 Comment(4)
That works for providing quotes on the start in field, However, the target field still shows: \\UNCPATH1\Directory\application.exe argumentA ArgumentB Getting there... I tried the double and triple quotes, wrapping in a single quote and couldn't get the target formatted properly.Antoniettaantonin
Does the path you are using require quotes? If it doesn't then it won't apply them.Fastigiate
Interesting. I modified the target path and thus start in path to include a space and it did indeed add the quotes around the application. No way to make it pass those if not explicitly required? Also, of note, the working directory will auto complete to a quoted path with just single quotes if required. As posted above there will be double quotes around the start in path in the short cut. Will post updated code in follow up to original.Antoniettaantonin
Or use apostrophe: $Shortcut.TargetPath = '"\\UNCPATH1\Directory 1\application.exe"'.Fornax
H
2

You can escape the quote using `. It's the other symbol on the "~" key.

$Shortcut.TargetPath = "`"\\UNCPATH1\Directory\application.exe`""
Hernadez answered 4/8, 2015 at 18:36 Comment(4)
The tick does not force the quotes on an unspaced path. Looking for a method to force the quote in the "target" field of the shortcut. TheMadTechnician has the correct syntax if there is a space in the UNC path. And the code functions as posted for the "start in" with his syntax regardless of spacing in UNC path.Antoniettaantonin
@Antoniettaantonin For me, the quotes are retained with either a spaced or unspaced path. I may be missing what you mean by "forcing the quote".Hernadez
When I use what you have above: $Shortcut.TargetPath = ""\\UNCPATH1\Directory\application.exe"" I get \\UNCPATH1\Directory\application.exe argumentA ArgumentB I am trying to get: "\\UNCPATH1\Directory\application.exe" argumentA ArgumentB in the target field of the created shortcut. If there is a space in the UNC path the code posted will add the quotes around the target application path. if no space, no quotes.Antoniettaantonin
OK, just tried a little manual edit of the path and when I add quotes manually to a UNC path without spaces they are stripped upon clicking apply. Looks like Windows strips the quotes off of the target path if not needed, even when explicitly calling for them on creation.Antoniettaantonin

© 2022 - 2024 — McMap. All rights reserved.