PowerShell launch script in new instance

前端 未结 4 995
失恋的感觉
失恋的感觉 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.)

提交回复
热议问题