Execute ps1 File on Linux Terminal: Easy PowerShell Scripting

Posted on

Execute ps1 File on Linux Terminal: Easy PowerShell Scripting

Execute ps1 File on Linux Terminal: Easy PowerShell Scripting

This tutorial aims to guide you on how to Execute ps1 File on Linux Terminal. PowerShell is a powerful command-line tool and scripting language that extends beyond the Windows environment. It offers graphical support and allows you to automate tasks traditionally performed through a GUI, unlocking a wider range of possibilities. Follow the steps provided on this guide to learn how to Execute ps1 File on Linux Terminal.

What is the PS1 command?

A PS1 file is essentially a script, or cmdlet, used by Windows PowerShell. It contains commands written in the PowerShell scripting language. These files can be executed directly from the PowerShell console or through the graphical user interface (GUI) of the operating system. The ability to Execute ps1 File on Linux Terminal expands the utility of PowerShell scripts.

Now, let’s dive into the steps required to install and run PowerShell scripts on a Linux distribution from the command line, enabling you to Execute ps1 File on Linux Terminal.

Before you begin, ensure you have access to your server as a non-root user with sudo privileges. This allows you to perform administrative tasks necessary for installation and execution. If needed, you can find resources on initial server setup for Linux distributions online.

Let’s move on to the detailed steps to accomplish this.

Execute ps1 File on Linux Terminal

Step 1 – How To Install PowerShell on Linux?

The first crucial step is to install PowerShell on your Linux distribution. The installation process varies depending on your specific distribution. Here are links to guides that cover installation on AlmaLinux, RHEL, Ubuntu, and Debian:

Follow the appropriate guide for your distribution. Once the installation is complete, proceed to the next step to Execute ps1 File on Linux Terminal.

Step 2 – How To Run PowerShell on Linux?

After successfully installing PowerShell, you can launch it from the Linux terminal using the following command:

pwsh

Upon execution, you should observe output similar to this:

**Output**
PowerShell 7.3.4
PS /root>

This indicates that you are now within the PowerShell environment. To exit PowerShell and return to the Linux terminal, use the following command:

exit

Step 3 – Run a .ps1 PowerShell Script from Linux Terminal

Now that you have PowerShell installed and know how to access it, you can run PowerShell scripts directly from the Linux terminal.

If you have an existing PowerShell script, you can execute it. If not, let’s create a simple one. Open your preferred Linux text editor (e.g., vi, nano, or gedit). In this example, we’ll use vi and name the file DrivesInformation.ps1:

sudo vi DrivesInformation.ps1

Add the following command to the file:

Get-PSDrive

This script will enumerate all of the PowerShell drives. Save and close the file.

To run the .ps1 file from the Linux terminal, use the following command:

pwsh <mark>path-to-your-script-file</mark>

For instance:

pwsh ~/DrivesInformation.ps1

The output will resemble:

**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 completes the process of executing a PowerShell script from the Linux terminal!

Conclusion

You have now successfully learned how to Execute a .ps1 File on Linux Terminal. This method allows you to create and execute PowerShell scripts directly from your bash environment, expanding your automation capabilities.

For further information, refer to the official PowerShell Documentation.

Alternative Solutions to Execute ps1 File on Linux Terminal

While using pwsh to directly execute .ps1 files is a straightforward method, there are alternative approaches that can be useful in specific scenarios. Let’s explore two different ways to achieve similar results.

1. Converting PowerShell to Bash using sh compatibility

PowerShell Core (pwsh) has enhanced cross-platform compatibility. You can sometimes execute a PowerShell script (with modifications) using the sh compatibility layer. This requires rewriting parts of the PowerShell script into compatible bash commands. This is not a direct execution of the .ps1 file but a conversion to a bash script.

Explanation:

This method involves analyzing the PowerShell script and identifying the key functionalities. Then, equivalent bash commands are used to replicate those functionalities. This approach is most suitable for simple PowerShell scripts that primarily interact with the file system or execute basic commands.

Example:

Let’s say you have a PowerShell script Get-Date.ps1 that simply retrieves the current date and time:

Get-Date

To achieve the same result using bash, you could create a bash script get_date.sh with the following content:

#!/bin/bash
date

You would then execute this script using:

bash get_date.sh

Important Considerations:

  • This method requires a good understanding of both PowerShell and bash scripting.
  • Complex PowerShell scripts with advanced features or cmdlets may not be easily translated to bash.
  • This is more of a rewrite than a direct execution.

2. Using Docker Container with PowerShell

Another approach is to use a Docker container with PowerShell installed. This creates an isolated environment where the PowerShell script can be executed without affecting the host system.

Explanation:

Docker containers provide a consistent and reproducible environment for running applications. You can create a Docker image with PowerShell pre-installed and then use that image to run your PowerShell scripts. This approach is particularly useful when dealing with complex dependencies or when you want to ensure that the script runs in a specific environment.

Example:

First, create a Dockerfile:

FROM mcr.microsoft.com/powershell:latest

WORKDIR /app

COPY . .

CMD ["pwsh", "your_script.ps1"]

Replace your_script.ps1 with the name of your PowerShell script. Then, build the Docker image:

docker build -t powershell-runner .

Finally, run the container:

docker run powershell-runner

Important Considerations:

  • Requires Docker to be installed on your system.
  • Adds an overhead due to the containerization process.
  • Provides a consistent and isolated environment for script execution.

These alternative solutions offer different ways to execute PowerShell scripts or achieve similar results on a Linux system. The best approach depends on the complexity of the script, the desired level of isolation, and your familiarity with bash scripting and Docker.

Leave a Reply

Your email address will not be published. Required fields are marked *