5

So I am running a basic script to copy a shortcut to the public profile desktop so that any user who logs in will have that on their desktop. The circumstances are that I will have to bypass execution policies, so I am doing this via batch file. This is what I have tried but it doesnt seem to work for me...

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList 'ExecutionPolicy Bypass -File DesktopShortcut.ps1' -Verb RunAs}"

and the PS file is simply:

Copy-Item -Path "aiStarter.lnk" -Destination "C:\Users\Public\Desktop\" -PassThru

When I run it the window just flashes then disappears. If I run it without RunAs I get access denied. I hate to ask this because I'm sure it has been asked before but I am pretty sure I am executing this correctly. Thoughts?

4
  • That looks strange. You are basically using a batch file to launch powershell, which launches powershell again which finally tries to run your powershell script. Commented Aug 11, 2016 at 19:45
  • 2
    Also, you don't need scripts to distribute shortcuts. Just use Group Policy. Commented Aug 11, 2016 at 20:59
  • 1
    @Squashman That convoluted syntax is required to launch a PowerShell process with elevated permissions. powershell.exe doesn't have a "runas" switch, so you need to leverage the Start-Process cmdlet. Commented Aug 11, 2016 at 23:01
  • Not sure if that's a typo in your question or in your actual code, but it should be -ExecutionPolicy in the argument list, not ExecutionPolicy (without leading hyphen). However, I agree with @Bill_Stewart that group policy (preferences) is a better way to do what you want. Commented Aug 11, 2016 at 23:07

1 Answer 1

6

Like a few others have said, this isn't the best choice for delivering shortcuts.

That being said, there are two issues. One is needing a - at execution policy. The other is after the elevated powershell instance is created the working path changes. So you need to add the full path to the script, which you can do with %~dp0 if it's a batch file.

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0DesktopShortcut.ps1' -Verb RunAs}"

Same likely needs to be done with the powershell script afterwards.

Copy-Item -Path "$($MyInvocation.MyCommand.Path)\aiStarter.lnk" -Destination "C:\Users\Public\Desktop\" -PassThru

This assumes the batch file, the short cut, and the powershell script are all in the same folder.

Sign up to request clarification or add additional context in comments.

2 Comments

Is the link file a link to DesktopShortcut.ps1? Why?
This answer works for me. However, I have a long ArgumentList to pass and I want to split if over multiple lines. Using a caret ^ does not work here. Example: Powershell.exe -Command "& {Start-Process Powershell.exe -Verb RunAs -ArgumentList '-ExecutionPolicy Bypass -File %~dp0HelloWorld.ps1 ^ -parameter1 Long ^ -parameter2 list ^ -parameter3 of ^ -parameter4 parameters ^ '}" (The new line feed after each ^ is not displayed in this comment)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.