Run Powershell Commands from a .bat file in Windows 11

837 views Asked by At

I use a .bat file to run a Powershell script. This worked in Windows 10, but in Windows 11 nothing happens.

@echo off
set INPUT=
set /P INPUT=   Please enter Username: %=%
runas.exe /user:%USERDOMAIN%\%INPUT% "powershell.exe  -WindowStyle hidden C:\Toolbox\Main.PS1"
set INPUT=

I have put in my credentials (it does ask for login and password). Normally it would run the power script with my uplift account and launch a box for me. Now it appears the script is not running at all.

How can I fix this?

2

There are 2 answers

1
AudioBubble On

you can use save the your file execution command in the .bat file (as shown below) with -ExecutionPolicy Unrestricted and if you click on .bat, it will call the PowerShell file and start to execute.

@echo off
 
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\PowerShell\example.ps1'"
 
TIMEOUT /T 10
2
Mike D On

This is a known MS bug tracked here:

https://github.com/microsoft/terminal/issues/12464

There is also a longish work-around there if you need it.

I was looking for a solution to a similar problem myself and, sadly, only found the above discussion.

EDIT: this basically means that your script may be running, just the window isn't getting hidden as expected. Hopefully I understood your problem correctly.