Introduction to PowerShell on Linux for Beginners

Aditya GadhaveAditya Gadhave
4 min read
  • PowerShell is a task automation and configuration management framework from Microsoft.

  • It consists of a command-line shell and a scripting language built on .NET, primarily designed for system administrators and IT professionals.

  • PowerShell allows users to automate and manage a wide range of tasks, from simple file operations to complex enterprise-level configurations, across various platforms. Here’s a detailed breakdown of PowerShell:

History and Evolution

  • Origins: PowerShell was first released in 2006 as part of the Windows Management Framework. It was originally called "Monad" and was designed to address the limitations of traditional command-line interfaces (CLIs) like CMD.

  • Versions:

    • Windows PowerShell: The original version built on the .NET Framework and available only on Windows.

    • PowerShell Core: Introduced with version 6, this is a cross-platform version built on .NET Core, allowing it to run on Windows, macOS, and Linux.

    • PowerShell 7: The latest major version, combining the functionality of both Windows PowerShell and PowerShell Core. It runs on the cross-platform .NET 5+ runtime.

Key Features

a) Command-Line Shell

  • PowerShell functions as a robust command-line interface (CLI) where users can type commands interactively. It supports built-in commands called cmdlets, external programs, and scripts.

b) Scripting Language

  • PowerShell includes a rich scripting language that supports variables, loops, conditionals, functions, and error handling.

  • Scripts in PowerShell are saved with the .ps1 extension and can be executed by calling them from the PowerShell prompt.

c) Object-Oriented Pipeline

  • One of PowerShell's unique features is that it passes objects, not plain text, between commands in a pipeline (a sequence of commands connected by |).

d) Cross-Platform Support

  • PowerShell Core (from version 6 onwards) runs on multiple platforms including Windows, macOS, and Linux. This makes it possible to use the same automation tools across different operating systems.

e) Remote Management

  • PowerShell provides PowerShell Remoting through the Invoke-Command cmdlet, which allows you to run commands on remote systems. It uses Windows Remote Management (WinRM) for Windows systems and SSH for non-Windows systems.

To install PowerShell on CentOS follow these steps:

1. Update CentOS Package List

First, ensure that your system is up to date by running the following command:

sudo yum update -y

3. Install PowerShell

Once the Microsoft repository is enabled, you can install PowerShell using dnf (for CentOS 8) or yum (for CentOS 7):

sudo yum install powershell -y

4. Start PowerShell

After installation is complete, you can start PowerShell by typing:

pwsh

5. Verify Installation

To verify that PowerShell is installed and check the version, run:

pwsh --version

This should display the installed version of PowerShell.

6. Update PowerShell

If a newer version is released and you want to update PowerShell, run:

sudo yum update powershell

7. Uninstall PowerShell

If you want to remove PowerShell from your system, use the following command:

sudo yum remove powershell

Common PowerShell Concepts

a) Cmdlets

  • Cmdlets are the basic building blocks of PowerShell. They are lightweight, single-purpose commands that follow a Verb-Noun pattern (e.g., Get-Process, Start-Service).

  • Cmdlets are consistent in their design, with uniform syntax, parameters, and output behavior.

b) Variables and Data Types

  • Variables in PowerShell are denoted by $, followed by the variable name (e.g., $myVar). PowerShell is dynamically typed, but you can specify types if needed. Example:

      $stringVar = "Hello, World"
      $intVar = 42
      [int]$explicitInt = 100
    

c) Pipelines and Objects

  • PowerShell cmdlets output objects, not text. This object-oriented approach allows for sophisticated data manipulation, where the output of one command can become the input of another. Example:

      Get-Service | Where-Object { $_.Status -eq 'Stopped' }
    

d) Loops and Conditionals

  • PowerShell supports standard control structures like if, for, foreach, and while loops for automation tasks. Example:

      foreach ($file in Get-ChildItem "C:\Temp") {
          if ($file.Length -gt 10MB) {
              Write-Host "Large File: $file"
          }
      }
    

e) Error Handling

  • PowerShell provides structured error handling using try, catch, and finally blocks, similar to other programming languages. Example:

      try {
          Get-Process -Name "notepad"
      } catch {
          Write-Host "Process not found!"
      } finally {
          Write-Host "Operation complete"
      }
    

    PowerShell Use Cases

    • System Administration: Automate tasks such as user management, file management, software installation, and patch management.

      • Example: Creating user accounts or configuring servers.
    • DevOps: PowerShell integrates well with automation tools like Ansible, Puppet, and Jenkins for managing infrastructure as code.

    • Cloud Management: PowerShell supports cloud platforms like Azure through the Az module, allowing you to manage cloud resources.

      • Example: Deploying virtual machines, managing storage accounts, etc.
    • Automation: From simple automation tasks (like batch file processing) to complex workflows, PowerShell can automate repetitive tasks across systems.

Conclusion:

PowerShell is a powerful tool that integrates the flexibility of scripting with the capabilities of the .NET framework, making it indispensable for system administrators and developers. Its ability to handle tasks across different platforms and its integration with cloud environments further solidifies its role in modern IT infrastructure and automation.

0
Subscribe to my newsletter

Read articles from Aditya Gadhave directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Gadhave
Aditya Gadhave

👋 Hello! I'm Aditya Gadhave, an enthusiastic Computer Engineering Undergraduate Student. My passion for technology has led me on an exciting journey where I'm honing my skills and making meaningful contributions.