0

I want to create a file with PowerShell and did it but I can not run this script from batch file. It just working on where is script located but I want to run this script another machine.

Batch file

PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""\\169.254.137.118\Shared\Greetings.ps1""' -Verb RunAs}"

PowerShell file

New-Item C:\Test.txt

4
  • What error are you getting? What happens or what error do you get when run without the Verb Runas parameters? Commented Mar 4, 2023 at 18:06
  • There is no error and changes without runas parameter. The PS just opens then closes.
    – jimowi9048
    Commented Mar 4, 2023 at 19:03
  • It's just working on local machine. I want to run this script on my machine.
    – jimowi9048
    Commented Mar 4, 2023 at 19:24
  • How about running it in this format PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""\\169.254.137.118\Shared\Greetings.ps1""' -Verb RunAs}" and also try that same format without the -Verb RunAs and see what happens. Perhaps you need to bypass execution policy on the initial call before the other powershell call within it. Commented Mar 5, 2023 at 4:48

2 Answers 2

0

The batch file must be Run as Administrator to start with.

There is no way for a non-administrator batch file to elevate itself to administrator status.


If this is already the case and you only wish powershell.exe to wait for the Start-Process command to finish, you need to add the -Wait parameter.

Also see the advice in the post
How to tell PowerShell to wait for each command to end before starting the next?

5
  • As you said. It is already elevated.
    – jimowi9048
    Commented Mar 4, 2023 at 19:04
  • Then it's normal for the PowerShell command to start the process and finish itself, as the process is then run independently. If you want it to wait, add the -Wait parameter. Also see the advice in the post How to tell PowerShell to wait for each command to end before starting the next?.
    – harrymc
    Commented Mar 4, 2023 at 19:24
  • It's just working on local machine. I want to run this script on my machine.
    – jimowi9048
    Commented Mar 4, 2023 at 19:25
  • See my above comment.
    – harrymc
    Commented Mar 4, 2023 at 19:25
  • Sorry, not working with this. It not creating the file on another machine.
    – jimowi9048
    Commented Mar 4, 2023 at 19:28
0

Just a note: If you compile the BAT file as an EXE, the program will automatically run as administrator.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .