Share your love
Execute .ps1 File on Linux Terminal
This Tutorial intends to teach you to Execute a .ps1 PowerShell Script File on Linux Command Line Terminal. PowerShell is a command-line tool that includes graphical support from a scripting language. With PowerShell Scripting you can complete all the tasks you do with GUI and much more.
What is the PS1 command?
A PS1 file is a script, or cmdlet, used by Windows PowerShell. It includes commands written in the PowerShell scripting language. PS1 files can be executed from the PowerShell itself, or the GUI of the operating system.
Now follow the steps below to see how to install and run PowerShell scripts on a Linux distro from the command line.
Steps To Execute .ps1 PowerShell Script File on Linux Terminal
To execute PowerShell scripts, you must have access to your server as a non-root user with sudo privileges. To do this, you can search the OrcaCore website for the Initial Server Setup for Linux Distros.
Now follow the steps below to complete this guide.
Step 1 – How To Install PowerShell on Linux?
First of all, you must install PowerShell on your Linux distro. Here you can follow these guides to install PowerShell on AlmaLinux, RHEL, Ubuntu, and Debian distros.
Install PowerShell on AlmaLinux 9 and RHEL 9
How To Install PowerShell on Ubuntu 22.04
How To Install PowerShell on Debian 11
When you are done with your installation, proceed to the next steps.
Step 2 – How To Run PowerShell on Linux?
When you have completed your PowerShell installation, you can simply run your PowerShell from the Linux terminal by executing the command below:
pwsh
In your output, you should see:
Output
PowerShell 7.3.4
PS /root>
To exit from PowerShell, run the command below:
exit
Step 3 – Run a .ps1 PowerShell Script from Linux Terminal
At this point, if you have a PowerShell Script, you can directly run it from the Linux terminal instead of the PowerShell console.
First, you need to open your favorite Linux text editor, we use vi editor and name the file as DrivesInformation.ps1:
sudo vi DrivesInformation.ps1
Add the following command to the file:
Get-PSDrive
This script will simply enumerate all of the PS drives. When you are done, save and close the file.
Then, you can simply run your .ps1 file by executing the following command:
pwsh path-to-your-script-file
For example:
pwsh ~/DrivesInformation.ps1
Output
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
/ 7.77 40.89 FileSystem /
Alias Alias
Env Environment
Function Function
Temp 7.77 40.89 FileSystem /tmp/
Variable Variable
That’s it.
Conclusion
At this point, you have learned to Execute a .ps1 File on Linux Terminal. With this method, you can easily build your PowerShell script file and run it from the bash.
For more information, you can visit PowerShell Documentation.
Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and Twitter.