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.
Asked
Active
Viewed 3.0k times
8

hithesh
- 321
- 2
- 4
- 13
-
So you run .\scriptfile.ps1 and your error says file.ps1 not found?? – Doug Maurer Aug 16 '20 at 01:34
-
No. It runs fine, no errors. The file not found error is when I run it inside the .sh script. – hithesh Aug 16 '20 at 02:12
4 Answers
9
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

Elroy Flynn
- 3,032
- 1
- 24
- 33
-
-
Adding #!/usr/bin/env pwsh, give me #!/usr/bin/env : bad interpreter: no such file or directory – hithesh Aug 16 '20 at 02:37
-
Also pwsh ./scriptfile.ps1, fails. pwsh not recognized as name of a cmdlet.....etc. Just .\scriptfile.ps1 by itself runs without errors. However, it fails when it's run from inside the .sh script. – hithesh Aug 16 '20 at 02:38
8
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

user829755
- 1,489
- 13
- 27
-
For me `powershell ./scriptfile.ps1` was the only thing that worked. `powershell scriptfile.ps1` gave an error and `powershell 'scriptfile.ps1'` gave an error. – Infineight Jul 26 '21 at 11:49
1
try to change permission and make it excutable with chmod +x file.ps1

Risna Fadillah
- 9
- 7
-
I tried that before, but I get chmod is not recognized as the name of a cmdlet, function, script file etc. – hithesh Aug 16 '20 at 00:53
-
-
-
I get the same message again when I run $ which pwsh ($ not recognized as the name of cmdlet.....). Also tried which pwsh. (which not recognized as the name of cmdlet etc) – hithesh Aug 16 '20 at 01:13
-
@hithesh It sounds like you are running these commands from inside of the Powershell shell; try running them from your default shell (Bash, ZSH, etc). – Scott Simontis Aug 19 '20 at 04:04
-
@Scott Simontis This is on windows pc. So powershell is all I have. No bash etc. – hithesh Aug 20 '20 at 06:34
0
# 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

devops-admin
- 1,447
- 1
- 15
- 26