简体   繁体   中英

How to run powershell script (.ps1) in windows terminal powershell tab?

I have tried making the script open with windows terminal (wt.exe) but it displays the error:

[error 0x800700c1 when launching `"C:\Users\hjdom\OneDrive\Desktop\All desktop stuff\Python Game\RunGame.ps1"']

I really need it to run in the windows terminal because it allows you to keep typing commands after the script is over. I am using the script to run a python file.

this is what the ps1 script contains:

python ./pythonfiles/startgame.py

Thanks so much! -BlueFalconHD

The Windows Terminal (wt.exe) is not a command shell.

It is a host for command shells (powershell, bash, cmd, python, perl, etc.). You have to tell WT.exe which to use, to call your script.

Example - cmd.exe or via the WinKey Run:

Type:

wt d:\temp\hello.ps1

this will fail with...

[error 0x800700c1 when launching `d:\temp\hello.ps1']

... even though Windows Terminal launches with whatever default shell is in your WT settings.

Now do this...

wt.exe powershell D:\Scripts\hello.ps1

... this works, because you told wt.exe which shell to use to execute your script.

Update as per your comment.

but then how do I prevent the terminal from closing after the script is done running?

Your target shell needs to provide a method for that. Powershell (Windows and Core) provide this via the -NoExit switch.

powershell /?

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

wt.exe powershell -NoProfile -NoLogo -NoExit D:\Scripts\hello.ps1

wt.exe pwsh -NoProfile -NoLogo -NoExit D:\Scripts\hello.ps1

If you are calling another shell, bash, python, perl, etc., then they too would need to provide that.

For everyone that wants to personalise their PowerShell terminal inside windows terminal or run a script when PowerShell is opened inside windows terminal, you must do the following:

You must run the desired script as an argument passed to PowerShell. In other words, you use the powershell.exe as an medium through which you run the script and not as the medium in which the script is run. This means that once the chosen script finished executing, powershell.exe will stop too because the executable's functionality was passed to the script. In order to prevent this you must pass to the argument the attribute " PowerShell -NoExit ".

Example:


%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe PowerShell -NoExit %SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\PowerShell_Startup_Ascii_Art.ps1

This will prevent PowerShell from closing, once the script finished executing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM