Getting Started with PowerShell on Linux: A Beginner’s Guide

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

This article will take you through the steps of creating your first PowerShell script on Linux, which is a requirement for anyone who wants to automate their work with this amazing tool.

The “powershell linux commands cheat sheet” is a guide to getting started with PowerShell on Linux. It includes information about the basic commands and how they work.

Getting Started with PowerShell on Linux: A Beginner's Guide

Microsoft PowerShell was often assumed to be a Windows-only tool, but that is no longer the case. With PowerShell v6, all of PowerShell’s features are now accessible cross-platform, with Linux being the most popular.

In this video, you’ll learn how to install PowerShell on Ubuntu (and other Linux distributions), perform commands, and even install some modules.

Let’s get this party started!

Prerequisites

This presentation will include a variety of demonstrations. If you want to follow along, make sure you have the following items on hand:

Repository of Packages is being updated.

You must first update the apt package manager’s repository list before PowerShell on Linux Installation. PowerShell is one of the Linux distributions that uses the apt package manager to install apt packages.

To ensure that apt can find the PowerShell package and any dependency when downloading, you must first update apt’s package repository list. To do that, run [apt update](<https://www.cyberciti.biz/faq/what-does-sudo-apt-get-update-command-do-on-ubuntu-debian/>).

PowerShell on Linux Installation

It’s time to install PowerShell on Linux now that you’ve updated the package repositories. Because this guide uses an Ubuntu-based distribution, there are two options for installing PowerShell: snap packages or the.NET SDK.

Using Snap Packages to Install PowerShell

Snaps are the most convenient method to install PowerShell on Ubuntu. Snaps are software packages that run on a variety of Linux versions and distributions.

Open a terminal and perform the snap install Linux command with sudo access using the snap package name of powershell to install PowerShell on Linux through a snap package.

Snap packages are restricted in scope by default for security concerns. You may make modifications to your system outside of the security sandbox if you use the —classic flag.

powershell —classic sudo snap install

PowerShell on Linux Installation using SnapPowerShell on Linux Installation using Snap

Using the.NET SDK to install PowerShell

1. The PowerShell snap package may not always include the most recent version of PowerShell. If this occurs, you may always go with the.NET SDK route, which is constantly updated.

Because PowerShell is included with.NET Core, when you install the.NET SDK, you’ll also obtain PowerShell.

Using the apt package manager, execute the following command to download and install the latest available.NET SDK. The most recent.NET SDK package is v5.0.8 as of this writing. You may get the most recent version here.

dotnet-sdk-5.0 sudo apt install

On Ubuntu, installing the latest.Net SDKOn Ubuntu, installing the latest.Net SDK

2. Next, use the dotnet tool install to install PowerShell using the dotnet application. PowerShell is packaged as a NuGet package, as specified by the dotnet tool command.

Dotnet installs the PowerShell package to.dotnettools and ensures that the directory is in the system PATH so you can run it from anywhere if you use the —global option.

install dotnet tool —global PowerShell

Using the.NET SDK to install PowerShellUsing the.NET SDK to install PowerShell

On Linux, Executing Basic PowerShell Commands

What’s next once you’ve installed PowerShell on Linux? Let’s get started with some fundamental commands. Simply type pwsh to launch PowerShell and you’ll be taken to the PowerShell interactive interface.

On Linux, you may use PowerShell.On Linux, you may use PowerShell.

PowerShell on Linux supports all of the well recognized commands from CMD and Linux’s command line shell, such as sudo apt update, since it is a cross-platform scripting language. There’s no need to start a Bash terminal!

Using PowerShell core to update Linux package repositoriesUsing PowerShell core to update Linux package repositories

You can perform hundreds of built-in PowerShell commands from here on out. While you’re at it, use $PSVersionTable to see what version of PowerShell is installed. The PSEdition should be Core, and the Platform should be Unix.

On Linux, you may check the version of PowerShell core that is installed.On Linux, you may check the version of PowerShell core that is installed.

When you’re finished in the PowerShell console, use the exit command to quit. You’ll be returned to your Bash terminal using this command.

Limitations of PowerShell on Linux

It’s not true that just because you can install PowerShell and execute commands on it, you can do anything on Windows. After all, Linux is a whole different operating system than Windows, and certain commands will be unavailable for capabilities that aren’t present in Linux.

Linux -ne Windows

You won’t notice any PowerShell drives on Linux, for example, since it lacks a registry. The Get-PSDrive cmdlet will continue to work, but it will only return mounted Linux storage volumes and regular PS Drives. Here’s an example of what I’m talking about.

Format-Table -Auto | Get-PSDrive

PowerShell Core's Get-PSDrive cmdletPowerShell Core’s Get-PSDrive cmdlet

Linux does not have a Common Information Model (CIM) or Windows Management Instrumentation, unlike Microsoft Windows (WMI). Both of these help the Windows operating system get information about how to handle devices and apps.

The Get-CimInstance cmdlet from Windows PowerShell is not accessible in PowerShell on Linux, as seen below.

Get-CimInstance Win32 BIOS -ClassName

Using PowerShell Core on Linux to get BIOS information through CIMUsing PowerShell Core on Linux to get BIOS information through CIM

However, if you attempt Get-CimInstance in Windows PowerShell, you’ll notice that it works correctly.

Using CIM with Windows PowerShell to get BIOS informationUsing CIM with Windows PowerShell to get BIOS information

There are no scheduled jobs.

The absence of scheduled tasks is another disadvantage of PowerShell on Linux. Scheduled jobs are a useful feature in Windows PowerShell that lets you conduct operations in the background using the Windows Task Scheduler.

How to Use PowerShell to Create and Manage Scheduled Tasks

To create a scheduled task in Windows PowerShell, use the Register-ScheduledJob cmdlet to register the job. Below is an example of how to transfer various Excel workbooks using Windows PowerShell. You’ll see that the cmdlet Register-ScheduledJob is missing.

On Linux, registering a scheduled job with PowerShell CoreOn Linux, registering a scheduled job with PowerShell Core

Although you can’t schedule a task, you may still use the Start-Job command to start one. The Start-Job cmdlet in PowerShell enables you to launch background tasks that run without interfering with the current session.

You may want to list all of the files in /var/backups in the background. You may put the command in a ScriptBlock and use Start-Task to start a job named GetAllBackupFiles, as seen below.

Start-Job -Name GetAllBackupFiles -ScriptBlock Get-ChildItem /var/backups -Recurse Start-Job -Name GetAllBackupFiles -ScriptBlock Get-ChildItem /var/backups -Recurse

To acquire a list of files in the /var/backups directory, start a background task.To acquire a list of files in the /var/backups directory, start a background task.

You can then use the Retrieve-Task cmdlet to get the output from the background job by giving the -Name that you gave when initiating it.

GetAllBackupFiles -Name Retrieve-Job

Getting the results of the background job.Getting the results of the background job.

Using PowerShell Modules on a Linux System

The PowerShell module is a crucial element in Windows PowerShell. PowerShell modules are collections of commands that may be downloaded as “packages” from Microsoft or from the PowerShell community.

Understanding and Creating PowerShell Modules is a related topic.

Locating Modules That Have Been Installed

PowerShell on Linux, like PowerShell on Windows, installs a number of modules by default. Run the Get-Module command to find all of those modules, as shown below. Get-Module is told to return all modules on the filesystem, not just loaded modules, using the ListAvailable option.

Get-Module -AvailableList

Pester is installed at /root/.local/share/powershell/Modules, which is the user location for PowerShell modules, as seen below. In the next part, you’ll learn how to install modules.

On Linux, finding the location of a saved PowerShell moduleOn Linux, finding the location of a saved PowerShell module

The PSModulePath environment variable, as seen below, shows each place where modules may be installed. Because the value of $env:PSModulePath is a single string, the tutorial splits the string on colons (rather to semicolons as in Windows) to show each path on its own line.

$env:PSModulePath -split ‘:’ $env:PSModulePath -split ‘:’ $env:PSModul

Module locations in PowerShellModule locations in PowerShell

On Linux, how to install PowerShell Modules

Even though PowerShell on Linux comes with a number of modules pre-installed, you’re likely to want additional capabilities. When this occurs, you may use the PowerShell Gallery to help you out. The PowerShell Gallery is a collection of thousands of modules that are compatible with both Windows and Linux.

You can install modules on Linux using the Install-Module cmdlet, just as you do on Windows. But first, use Find-Module to locate the module you’re searching for.

Let’s imagine you want to use the famous Pester testing framework to run some Pesters tests. Pester is delivered as a module. Using the Find-Module cmdlet, determine whether the Pester module is present in the PowerShell Gallery.

Find-Module discovered the accessible Pester module, as shown below.

Module Source VerificationModule Source Verification

After you’ve confirmed that the Pester module is accessible, use the Install-Module cmdlet to install it by supplying the module’s name.

Pester -Name Install-Module

Because the repository is designated as untrustworthy, PowerShell will notify you about an untrusted repository when you perform Install-Module. This warning is unimportant for testing purposes, so enter A to confirm.

Module InstallationModule Installation

Run the Get-InstalledModule cmdlet once the module has been installed, and you’ll notice that Pester has been installed and its commands are now accessible.

On PowerShell Core, getting a list of installed modules.On PowerShell Core, getting a list of installed modules.

You’ll also see that you can locate all commands within the Pester module using the Get-Command cmdlet, as shown below.

Pester -Get-Command -Module

The Pester Module provides a number of commands.The Pester Module provides a number of commands.

Using Bash to Run PowerShell Scripts

To wrap up this tutorial, let’s end with Using Bash to Run PowerShell Scripts. Throughout this tutorial, you’ve been interactively running PowerShell commands inside of a PowerShell console. If you have a PowerShell script you need to execute, there’s no need to drop into the PowerShell console. Instead, you can execute the script directly from Bash.

To illustrate launching a PowerShell script from Bash, paste the following command into your preferred Linux text editor and save the file as DrivesInformation.ps1. The purpose of this script is to list all of the PS drives.

Instead of typing pwsh, pressing Enter, and then executing the script in the PowerShell console, execute the script straight from your Bash terminal. Run pwsh once again, but this time specify the PowerShell script you just built, as seen below.

You’ll see that you get the same output in your Bash terminal as you would in the interactive PowerShell session.

pwsh /home/tex/DrivesInformation.ps1 /home/tex/DrivesInformation.ps1 /home/tex/DrivesInformation.p

Using the installed PowerShell core to run a PowerShell script from BashUsing the installed PowerShell core to run a PowerShell script from Bash

Conclusion

You should now be able to run PowerShell on Linux with ease! It’s now up to you to learn more about PowerShell and automate as much as possible!

The “run powershell script on linux” is a beginner’s guide to PowerShell. It teaches the basics of how to use PowerShell and get started with it.

Frequently Asked Questions

How do I start PowerShell in Linux?

A: To start PowerShell, do the following
chmod u+x pwsh.bat && ./pwsh

Can PowerShell be used in Linux?

A: PowerShell is a Microsoft Windows utility designed for system administrators. It can be used in Linux and other operating systems, but it might not work as intended without modifications.

How do I run PowerShell for beginners?

A: PowerShell is a versatile and powerful command-line tool that allows users to automate tasks on their computers. To run the most commonly used operations, all you need is a copy of Windows with an updated version installed and then it can be accessed by opening its Run prompt (Windows Key + R).

Related Tags

  • powershell on linux limitations
  • powershell core linux
  • install powershell on linux
  • azure powershell linux
  • linux powershell online

Table of Content