creating quoted path for shortcut with arguments in powershell

thatotheritguy picture thatotheritguy · Aug 4, 2015 · Viewed 16.1k times · Source

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)

Answer

TheMadTechnician picture TheMadTechnician · Aug 4, 2015

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.