creating quoted path for shortcut with arguments in powershell
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.
You can escape the quote using `. It's the other symbol on the "~" key.
$Shortcut.TargetPath = "`"\\UNCPATH1\Directory\application.exe`""