ArticleHow can I set up PowerShell on a Linux operating system?

How can I set up PowerShell on a Linux operating system?

Summary

  • Linux users can use PowerShell to execute scripts and commands that are object-oriented and cross-platform
  • Depending on the distro, PowerShell can be installed on Linux, although Microsoft provides the scripts for easy installation
  • On Linux, you can change your default shell to PowerShell with ease by using the chsh command

PowerShell is a command line language for Windows, but did you know that it can be used on Linux too? Learn how to use cross-platform scripting and commands

What is PowerShell?

PowerShell is a programming language that allows for the creation of scripts and commands Created by Microsoft as a replacement for the outdated Command Prompt. Although it’s primarily connected to Windows, the company has made it available for Linux as part of their ongoing efforts to make pigs fly

In Linux, PowerShell functions similarly to any other Shell for both scripting and command execution. Despite having strict default instructions, it can still recognize standard symbols like “ls” in Linux and “dir” on Windows to indicate a list of files. It’s not based on dependencies Text streams The complexity of this approach exceeds that of the conventional Unix Philosophy solution, which involves redirecting text input and output

What advantages does PowerShell offer for users on Linux?

The availability of PowerShell on Linux is a significant advantage. Bash and other classic shells like Java, JavaScript, Perl, Python, and Github are among the many scripting languages that Linux already has

For Linux users who may be managing a Windows server, PowerShell is the ideal choice. Perhaps you’re familiar with Windows?. The PowerShell command line could be a safety net for you as you start learning Linux

Installing PowerShell

The installation process of Powershell is dependent on the Linux distribution you choose

Microsoft Debian PowerShell installation page with copyable 829XX385scripture829XX385

Many well-known distros, such as Red Hat Enterprise Linux Family and Debian/Ubuntu Edition from the Microsoft Corporation, have their own repositories

Install it on Debian. Microsoft helpfully provides A text that can be pasted into a document They have a button that you can press to copy it to your clipboard

Plugging the script into your terminal emulator will enable PowerShell to be installed and run

Installation of PowerShell in the Debian terminal,

Micorosoft has Similar guidelines for most other popular Linux distributions

Starting PowerShell on Linux

PowerShell can be executed after the installation is finished. Use the following command to accomplish that::

  Pwsh  

The interactive nature of PowerShell is reminiscent of your login shell. It is capable of typing regular commands

While the ls command is functional, try using “dir” instead. You’ll see something different. The display resembles the output of the ls -l command, but it includes the Linux permissions of all the files in the left-hand column, user and group information, the last modified time, and finally the name of those files

Output of the PowerShell "dir" command in Linux.

When PowerShell is finished, simply type in “exit” or hold CTRL+d to close the session. The window will close automatically if you use PowerShell as your login shell. Running from a different shell will bring you back to the original shell

Changing Shells to PowerShell

The easiest way to switch the Linux shell you use when accessing a terminal window to PowerShell is through configuration

The list of shells in /etc/shells must contain Powershell. The list can be accessed by looking at Cat :

  Cat /etc/shells  
Output of "cat /etc/shells" command in Linux.

You ought to come across a line that starts with “pwsh”. Two partitions, /usr/bin/pwsh and 1/opt/microsoft/powershell/7/pmw, are present on this system. The first option appears to be more suitable, as it doesn’t include version numbers and is less prone to breaking updates

Use the To modify your login shell, use the chsh command When asked for your password, you’ll be prompted to provide it. Use the pathname option in the command prompt to navigate to /usr/bin/pwsh

If the shell in /etc/shells is legitimate, then entering the Enter key indicates that you’ve changed your login method

Leave, log back in, open a new terminal window (or virtual terminal), and you should be signed into PowerShell by default

The paths that are used in PowerShell scripts can also be used to specify Powershell A shebang line The term “shebang line” is named after two characters that start at the beginning, a hash (#) and an exclamation point (!). ”

To call PowerShell from a script, we’ll use the same path we did earlier at the very first line in the script:

   #! /usr/bin/pwsh   

This should ensure that the script is run with PowerShell. You’re now ready to explore PowerShell in Linux

Latest article

More article