nkasco
nkasco

Reputation: 336

Launch as Admin Powershell via Batch File

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?

Upvotes: 5

Views: 35965

Answers (1)

Patrick Meinecke
Patrick Meinecke

Reputation: 4173

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.

Upvotes: 6

Related Questions