- 1 year ago
Powershell to create shortcut with quoted target path
To create a shortcut with a quoted target path using PowerShell, you can use the New-Object
cmdlet along with the WScript.Shell
COM object. Here's an example:
powershell$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut("C:\Path\to\Shortcut.lnk")
$shortcut.TargetPath = '"C:\Path\to\Target.exe"'
$shortcut.Save()
In this example, you first create a new instance of the WScript.Shell
COM object using the New-Object
cmdlet. Then, you use the CreateShortcut
method of the $shell
object to create a new shortcut with the specified path (C:\Path\to\Shortcut.lnk
in this case).
Next, you set the TargetPath
property of the $shortcut
object to the target path enclosed in double quotes ("C:\Path\to\Target.exe"
in this case). The double quotes are added by using single quotes around the entire string to preserve the double quotes as part of the value.
Finally, you call the Save
method on the $shortcut
object to save the shortcut file.
Make sure to replace C:\Path\to\Shortcut.lnk
with the desired path and filename for your shortcut file, and C:\Path\to\Target.exe
with the actual path and filename of the target executable file.
When running this PowerShell script, it will create a shortcut with the specified target path, including the quoted path if necessary.