How to open another PowerShell console from a PowerShell script

2.2k views Asked by At

In OSX, I open a bash terminal and enter a PowerShell console. In my PowerShell script, I would like to open another PowerShell console and execute a PowerShell script there.

Under Windows, I would do

Invoke-Expression ('cmd /c start powershell -Command test.ps1')

How could I do the samething in OSX?

2

There are 2 answers

1
mklement0 On BEST ANSWER

To start a PowerShell instance in a new terminal window on macOS:


Without being able to pass arguments to it:

PS> open -a Terminal $PSHOME/powershell

If you want to run a given command:

Unfortunately, quite a bit more work is needed if you want to pass a command to run in the new PowerShell instance:
In essence, you need to place your command in a temporary, self-deleting, executable shell script that is invoked via a shebang line:

Note: Be sure to run at least PowerShell Core v6.0.0-beta.6 for this to work.

Function Start-InNewWindowMacOS {
  param(
     [Parameter(Mandatory)] [ScriptBlock] $ScriptBlock,
     [Switch] $NoProfile,
     [Switch] $NoExit
  )

  # Construct the shebang line 
  $shebangLine = '#!/usr/bin/env powershell'
  # Add options, if specified:
  # As an aside: Fundamentally, this wouldn't work on Linux, where
  # the shebang line only supports *1* argument, which is `powershell` in this case.
  if ($NoExit) { $shebangLine += ' -NoExit' }
  if ($NoProfile) { $shebangLine += ' -NoProfile' }

  # Create a temporary script file
  $tmpScript = New-TemporaryFile

  # Add the shebang line, the self-deletion code, and the script-block code.
  # Note: 
  #      * The self-deletion code assumes that the script was read *as a whole*
  #        on execution, which assumes that it is reasonably small.
  #        Ideally, the self-deletion code would use 
  #        'Remove-Item -LiteralPath $PSCommandPath`, but, 
  #        as of PowerShell Core v6.0.0-beta.6, this doesn't work due to a bug 
  #        - see https://github.com/PowerShell/PowerShell/issues/4217
  #      * UTF8 encoding is desired, but -Encoding utf8, regrettably, creates
  #        a file with BOM. For now, use ASCII.
  #        Once v6 is released, BOM-less UTF8 will be the *default*, in which
  #        case you'll be able to use `> $tmpScript` instead.
  $shebangLine, "Remove-Item -LiteralPath '$tmpScript'", $ScriptBlock.ToString() | 
    Set-Content -Encoding Ascii -LiteralPath $tmpScript

  # Make the script file executable.
  chmod +x $tmpScript

  # Invoke it in a new terminal window via `open -a Terminal`
  # Note that `open` is a macOS-specific utility.
  open -a Terminal -- $tmpScript

}

With this function defined, you can invoke PowerShell with a given command - specified as a script block - as follows:

# Sample invocation
Start-InNewWindowMacOS -NoExit { Get-Date }
1
Grady Player On

I don't know anything about powershell on mac, if that even exists but to open a gui application like a terminal on Mac OS X you can use the open command:

open -a /Applications/Utilities/Terminal.app "" would be a new blank window
open -a /Applications/Utilities/Terminal.app somescrip.sh would be to run a script

or you can make an apple script and run that

save the following in a file (~/OpenNewTerminal.scp):

tell application "Terminal"
    do script " "
    activate
end tell

then you can run it with osascript

osascript ~/OpenNewTerminal.scp

of course the more bash idiomatic way would be to run in a subshell or in the background

subshell:

output=$(ls)
echo $output

background:

./command &

background with redirected output so it doesn't bleed into your current shell:

./command 2>&1 > /dev/null