How to use PowerShell to create a SymbolicLink for an exe with a file path parameter?

1.5k Views Asked by At

I am trying to create a Windows desktop shortcut (SymbolicLink) that runs a PowerShell script. If this were executed from the command line, it would be:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noexit -file "C:\User\BFT\Test\HelloWorld.ps1"

And the command line does work with this.

I can also create a shortcut by right-clicking on the desktop, selecting New, selecting shortcut, and loading the above command into the dialog. This also works just fine.

When I attempt to create this shortcut in PowerShell using the command:

New-Item -ItemType SymbolicLink -Path "c:\User\BFT\Test" -Name "Test" -Target """C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noexit -file "C:\User\BFT\Test\HelloWorld.ps1"""

I receive an error saying

New-Item : A parameter cannot be found that matches parameter name 'noexit'.

I have tried several versions involving different quoting, with and without -noexit and -file, etc. None seem to work.

It would appear PowerShell can't deal with both the exe and the file path portion, but this seems like it should be a common use and I would think there is a way. I am probably missing something.

Any help in suggesting solutions would be greatly appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Shortcut files (.lnk files) aren't the same as symbolic links - see this answer for background information.

New-Item doesn't support creating shortcut files, only symbolic links,

To create shortcut files programmatically, you must use the WScript.Shell COM component, as shown in this answer.