5

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?

5
  • start powershell? Commented Sep 4, 2017 at 19:16
  • doesn't help :( Commented Sep 4, 2017 at 19:52
  • Are you using PowerShell 6 on OS/X from github.com/PowerShell/PowerShell Commented Sep 4, 2017 at 21:11
  • Yes, I am using PS6. Commented Sep 5, 2017 at 6:07
  • Even under Windows that's still an awful way to achieve that ... Commented Jan 15, 2018 at 19:18

2 Answers 2

2

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 }
Sign up to request clarification or add additional context in comments.

1 Comment

Glad to hear it, Jim (thanks for confirming, @LotPings). Yes, invocation via shebang lines was broken in earlier betas; I've added a note stating the minimum version requirement. Generally, it's worth updating as soon as a new release becomes available.
0

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

1 Comment

I try a few different things with 'open -a Terminal.app ', but no luck. I did not try the 2nd solution. I would like to open another powershell console and execute powershell script from a powershell script, I am not sure how to implement the 2nd.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.