How to Install PowerShell on Linux


Summary

  • PowerShell is available for Linux, allowing you to run cross-platform commands and scripts with object-oriented features.
  • Installing PowerShell on Linux depends on the distro used, with Microsoft providing scripts for easy installation.
  • You can easily change your default shell to PowerShell on Linux by using the chsh command.

You may think of PowerShell as the command-line interface and scripting language on Windows, did you know that you can use it on Linux? Here’s how to take advantage of cross-platform commands and scripting

What is PowerShell?

PowerShell is a command and scripting language created by Microsoft to replace the old Command Prompt. While it’s primarily associated with Windows, as part of the company’s continuing attempts to make pigs fly, they’ve made it available for Linux.

PowerShell can be used like any other shell in Linux to launch commands and to write scripts. While it has verbose commands by default, it understands common aliases like “ls” in Linux and “dir” in Windows to list a directory. It’s also object-oriented instead of relying on text streams. This makes it more complicated than the traditional Unix Philosophy solution of redirecting text input and output.

Why Install PowerShell on Linux?

The reason you might want to install PowerShell on Linux is that it gives you options. Linux already has lots of scripting languages, including the classic shells like Bash as well as more advanced languages like Perl and Python.

If you primarily use Linux but might have to administer a Windows server, you might benefit from having PowerShell installed on your Linux system. Or maybe you’re more familiar with Windows. The PowerShell command line could give you an anchor while you start exploring Linux.

Installing PowerShell

How you install Powershell will depend on what Linux distro you use.

Microsoft Debian PowerShell installation page with copyable script.

Microsoft provides its own repositories for many popular distros, including the Red Hat Enterprise Linux Family and Debian/Ubuntu

Let’s install it for Debian. Microsoft helpfully provides a script you can copy and paste. They even have a button you can click to copy it into your clipboard.

Paste the script into your terminal emulator, and it will install and run PowerShell.

Installation of PowerShell in the Debian terminal,

Micorosoft has similar instructions for most other popular Linux distros.

Starting PowerShell on Linux

Once you’ve gotten PowerShell installed you can run it. To do that, use this command:

        pwsh
    

You’ll be using PowerShell interactively, as you would for your login shell. You can type regular commands into it.

The ls command works as usual, but try using the “dir” command. You’ll see something different. You’ll see something similar to the output of the ls -l option, with the Linux permissions of the files in the left-hand column, user and group information, the last modified time, and finally the name of the files.

Output of the PowerShell "dir" command in Linux.

When you’ve finished with PowerShell, type “exit” or press Ctrl+d to end your session. If PowerShell is your login shell, this will close the window. If you’re running it from another shell, this will return you to the shell you called it from.

Changing Shells to PowerShell

If you want to change the Linux shell you use when you open a terminal window to PowerShell, it’s easy to do.

Powershell will have to be in the list of shells in the file /etc/shells. To see the list, use cat:

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

You should see a line that contains “pwsh.” On this system, there are two of them: /usr/bin/pwsh and /opt/microsoft/powershell/7/pwsh. The first one seems like a better bet, since it doesn’t reference any version numbers and is less likely to break with updates.

Use the chsh command to change your login shell. You’ll need to enter your password when prompted. At the prompt, enter the pathname you want to use, in this case, /usr/bin/pwsh.

Press the Enter key and if it’s a valid shell in /etc/shells, you’ve just changed your login shell.

Log out and log back in, and start up a terminal window or virtual terminal, and you should be logged into PowerShell by default now.

If you’re writing PowerShell scripts, you can also specify Powershell by using the same paths with a shebang line. This is named because a shebang line starts with two characters: a hash (#) and an exclamation point (!), also known as a “bang.”

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.



Source link

Previous articleBitcoin Falls Below $100K as Crypto Market Sheds $554 Million in Liquidations – Bitcoin.com News
Next articleMicroStrategy’s Saylor Reveals What Bitcoin (BTC) Really Is