PowerShell launch script in new instance

前端 未结 4 992
失恋的感觉
失恋的感觉 2020-12-09 08:34

I have a Master script that has several options. When you select 1 in the menu, action 1 will be executed and afterwards you\'ll get back to the menu. This

相关标签:
4条回答
  • 2020-12-09 08:38

    Although start powershell command looks cleaner, it doesn't allow you to do everything that you can do with invoke-expression. For example, the following opens a new window, changes its title and background color, and leaves the window open:

    invoke-expression 'cmd /c start powershell -NoExit -Command {                           `
        cd -path $env:homedrive$env:homepath/Documents/MySillyFolder;                  `
        $host.UI.RawUI.WindowTitle = "A Silly Little Title";                                `
        color -background "red";                                                            `
    }';
    

    It also runs fine from within another powershell script.

    If you try to do this using the start powershell {...} syntax it will give an error on the title-changing line, and won't keep the window open. (I suppose It's possible that there's some obscure syntax hack that will make start powershell work, but I haven't been able to find one.)

    0 讨论(0)
  • 2020-12-09 08:45

    Using cmd to start powershell?

    start-process powershell -ArgumentList '-noexit -command 'Commands for the new PowerShell''
    
    0 讨论(0)
  • 2020-12-09 08:47

    To launch in an external PS window, you can use the following:

    invoke-expression 'cmd /c start powershell -Command { [script block here] }'
    

    E.g.:

    invoke-expression 'cmd /c start powershell -Command { write-host "Hi, new window!"; set-location "C:\"; get-childitem ; sleep 3}'
    
    0 讨论(0)
  • 2020-12-09 08:57

    Instead of starting a cmd to start a new powershell instance you can:

    start powershell {echo hello}
    

    To prevent immediate exit of new started powershell:

    start powershell {echo hello; Read-Host}
    
    0 讨论(0)
提交回复
热议问题