Execute a powershell script from inside the git bash

taijidude - Apr 29 '22 - - Dev Community

I develop on Windows. And somehow got into the habit of using both the powershell and the git bash. It's not clear cut, but generally speaking i use git bash for small tasks and moving around the file system. When writing longer scripts i tend to use powershell.
This week i stumbled about a nice way to execute a powershell script from the bash. This helps to reduce context switches.

You have to use the following Statement

$ powershell -File scriptToRun.ps1
Enter fullscreen mode Exit fullscreen mode

When you use an alias it gets even more convinient

$ alias ps1='powershell -File'
$ ps1 scriptToRun.ps1
Enter fullscreen mode Exit fullscreen mode

Addition

Another way to run powershell scripts was suggested by a reader in the comments.

❗ First you have to make sure that the powershell.exe is on the path.

Then add the following shebang line at the start of your script:

#!/usr/bin/env pwsh
"Hello world!"
Enter fullscreen mode Exit fullscreen mode
$ ./helloWorld.ps1
Hello world!
Enter fullscreen mode Exit fullscreen mode

Thank you again @vitalytseshkovsky!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player