Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating quoted path for shortcut with arguments in powershell

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)

like image 544
thatotheritguy Avatar asked Aug 04 '15 17:08

thatotheritguy


2 Answers

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.

like image 86
TheMadTechnician Avatar answered Oct 03 '22 05:10

TheMadTechnician


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

$Shortcut.TargetPath = "`"\\UNCPATH1\Directory\application.exe`""
like image 37
Speerian Avatar answered Oct 03 '22 05:10

Speerian