hithesh
hithesh

Reputation: 449

Running powershell script inside a bash script

In Powershell-
How do I run a powershell script(.ps1) inside a bash script(.sh)
I can run just the powershell script-
& .\scriptfile.ps1
and just the bash script.
But when I try to run the powershell inside the bash script, I get
file.ps1 : command not found
Both scripts are in the same path.

Upvotes: 13

Views: 46544

Answers (5)

Jim Julian
Jim Julian

Reputation: 23

In my system, 'powershell' calls v5 and 'pwsh' calls v7. If v7 is installed, v5 is not removed. This may clear up some of the confusion. Also, wsl (Windows subsystem for Linux) may be able to call powershell and sudo in linux would be more appropriate than admin in pwsh if running pwsh from within bash.

Upvotes: 0

devops-admin
devops-admin

Reputation: 2003

# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

Upvotes: -1

user829755
user829755

Reputation: 1588

I'm using git bash on Windows 10 Pro and there it's powershell. The executable is in /c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe and it's contained in my $PATH. So I can say

powershell ./scriptfile.ps1
# or inline:
powershell 'my script'

In the Ubuntu-on-Windows bash I have to say

powershell.exe ./scriptfile.ps1

Upvotes: 9

Elroy Flynn
Elroy Flynn

Reputation: 3218

Are you trying

.\scriptfile.ps1

? That should be

./scriptfile.ps1

But also, when invoking powershell from a bash script, you'll need to either run the pwsh command like

pwsh ./scriptfile.ps1

or the first line of your Powershell script file should be a shebang (interpreter directive) like:

#!/usr/bin/env pwsh

See How can I use a shebang in a PowerShell script?

Upvotes: 14

Risna Fadillah
Risna Fadillah

Reputation: 9

try to change permission and make it excutable with chmod +x file.ps1

Upvotes: 1

Related Questions