Batch file of powershell commands runs in cmd not in powershell terminal - windows

I made a batch file to run with admin right in windows powershell, it opens a powershell but runs in another cmd windows. I saved the batch file with *.ps1 but it did not work.

Related

Continue With CMD Commands After Batch Script

I am trying to write a batch script which, once complete, would allow the user to continue using the Windows command prompt as they normally would had no script been run. Is this possible? Thank you in advance for any help.
If you manually open CMD (the Command Prompt) and invoke the batch file by name, CMD will remain open for additional commands after the batch file completes. You cannot do this by double-clicking on the batch file, but if you create a shortcut to the batch file that runs CMD.EXE with the /K switch, you will run the batch file and then leave CMD running for additional commands. See CMD at SS64.

How to run a windows bat file from unix script

Could you help me run the windows bat file from unix script
ftp_data()
{
ftp wsapp00012.srv.dir.abc.com << EOF
prompt off
asci
cd photo
cmd Photo_load.bat
quit
EOF
}
ftp_data
a .bat file is specifically a windows scripting file that usually calls windows applications within the .bat file. You should be able to open the bat file in any text editor and see what applications it calls to but you won't be able to run that on a Linux machine.
There is a slight chance that you could get the game to run using WINE, http://www.winehq.org/ but a game that uses batch files like that I would say you have a very slim chance.

How to "open with" in a batch file

I have a windows powershell script that will be available on a server to my users. I don't want them to have to go out and find the PS script, right click and click "run with powershell" or do an "open with". The Windows (Win 7 at least) default program is notepad.
I want to make a batch file to do this. I've tried:
start "c:\myfile.ps1" powershell.exe
and a few other variations, but all I've been able to do is either start powershell, or open my file in its default program, notepad.
Any advice is appreciated! Thanks!
Bonus question: If I run my batch file as administrator will it also run my PS script as administrator?
Simply use the -file argument for PowerShell.exe in your batch file:
PowerShell.exe -file c:\MyFile.ps1
Additionally, some users may have their Execution Policy set to something that would restrict scripts from being executed, so you may want to do something like:
PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1
If you would like to use start to launch it you can do so as Ansgar Wiechers noted by running:
start "" PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1
Some notes regarding using start: By default it will launch PowerShell in a separate window, and continue to execute the rest of the batch file without waiting for the PowerShell window to close. If that is undesirable you have two options. You can specify /wait which will wait for the PowerShell window to close before continuing the batch file, or you can use the /B option will will not open a new window, and will execute PowerShell in the current console window.
And finally, yes if your batch file is run under the Administrator context, PowerShell will be as well.

I have a bat file that needs to run in powershell, via the start-process command is slow

I have a powershell script that runs a bat file, it has to run in powershell as it is being called by an application that can only run powershell (not bat files)
I use the Start-Process command
start-process -FilePath D:\versioned\pf_trunk\deployment\environments\development\aws\deployment\awx\development\files\PublishMYPlanetFootprint.bat -Wait
The batch file runs some visual studio builds and then moves some files.
If I run the batch file on its own, it runs and completes and closes the command window.
When I run it in powershell, it runs, completes and closes the command window, however the script continues to run for another 17 minutes before timing out.
I thought the wait would only wait for the bat to finish and then terminate.
Can somebody help with my powershell. What am I missing to ensure the script terminates on completion of the bat file operation.
Thanks

Why is it that Cygwin can run .bat scripts?

When I execute a .bat script from bash in Cygwin, by what mechanism is it running? I understand that if I run a .EXE it will launch, regardless of whether the .EXE is from Cygwin or from a more traditional environment. I understand that when I execute an executable script with #! at the beginning that Cygwin supplies the magic for it to run.
But why does a .bat script work? Is there some component inside of Cygwin that is aware of what a Windows .bat script is and what to do with it? Or is it that it is somehow impossible under Windows to execute a call to launch a .EXE file that won't automatically also work for a .bat script instead?
Running
./test.bat params
from bash seems to be equivalent to
cmd /c test.bat params
I believe that bash in cygwin sees the bat extension as being flagged executable (a cygwin hit-tip to windows convention). As such it loads and executes the file with it's associated interpreter (cmd.exe, per os configuration), much as it creates a new instance of bash to run your #! scripts (per posix standard).
And if you want to fork an *.cmd file execution like a ShellScript process and append his log to an file:
cmd /c test.bat > nohup.out &
Enjoy!

Resources