This Tutorial intends to teach you to Execute a .ps1 File on Linux 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.
How To Execute .ps1 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.
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 execute the command below:
In your output, you should see:
Output PowerShell 7.3.4 PS /root>
To exit from PowerShell, run the command below:
Step 3 – How To Run a .ps1 File 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, here we use vi editor and name the file as DrivesInformation.ps1:
sudo vi DrivesInformation.ps1
Add the following command to the file:
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:
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
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.