TheCyberHub

Introduction to PowerShell (2023 Edition) – Basics and syntax

January 15, 2024 | by thecyberhub.net

Powershell

PowerShell is a task automation and configuration management framework developed by Microsoft, that consists of a command-line shell and associated scripting language built on top of the .NET framework. It’s designed to provide a powerful and flexible platform for automating administrative and other tasks.

PowerShell provides a rich set of commands, called cmdlets, which perform a variety of tasks, such as managing the file system, working with the registry, controlling Windows services, and managing Windows components such as Internet Information Services (IIS) and the Active Directory. PowerShell also allows you to write scripts that automate complex tasks and integrate with other technologies, such as the Microsoft Exchange Server, Microsoft SQL Server, and Microsoft SharePoint.

One of the main advantages of PowerShell is its ability to automate repetitive tasks, reducing the risk of human error and saving time. It also provides a way for administrators and developers to automate administrative tasks, monitor systems, and gather data for reporting. Additionally, it allows for easy integration with other technologies, making it a powerful tool for managing and automating complex IT environments.

PowerShell comes with a number of built-in modules that provide cmdlets for various tasks and functions. Some of the most commonly used built-in modules in PowerShell include:

  1. Microsoft.PowerShell.Core: This module provides basic functions and cmdlets that are used by all other modules.
  2. Microsoft.PowerShell.Management: This module provides cmdlets for managing the local system, such as managing the file system, services, processes, and the registry.
  3. Microsoft.PowerShell.Security: This module provides cmdlets for working with security-related tasks, such as managing certificates and creating digital signatures.
  4. Microsoft.PowerShell.Utility: This module provides a number of helpful cmdlets for working with strings, arrays, hash tables, and other objects.
  5. Microsoft.WSMan.Management: This module provides cmdlets for managing the Windows Remote Management (WinRM) service, which is used for remote administration of Windows systems.
  6. Microsoft.PowerShell.Host: This module provides cmdlets for working with the PowerShell host, including the ability to change the console font, background color, and cursor position.
  7. Microsoft.PowerShell.Diagnostics: This module provides cmdlets for working with performance and diagnostic data, such as collecting performance counter data and working with event logs.

In addition to the built-in modules, there are also many third-party modules available, which can be installed to extend the functionality of PowerShell. These modules can be found in the PowerShell Gallery, the Microsoft Script Center, and other online repositories.

If you’re working with Azure and Microsoft 365, there are a number of optional PowerShell modules that you may find useful. Some of these modules include:

  1. Azure Active Directory (AzureAD): This module provides cmdlets for managing Azure Active Directory, including tasks such as creating and managing users, groups, and applications.
  2. Azure Resource Manager (AzureRM): This module provides cmdlets for managing Azure resources, such as virtual machines, storage accounts, and networks.
  3. Exchange Online: This module provides cmdlets for managing Exchange Online, the cloud-based email and calendar service offered by Microsoft as part of Microsoft 365.
  4. SharePoint Online: This module provides cmdlets for managing SharePoint Online, the cloud-based collaboration and content management platform offered by Microsoft as part of Microsoft 365.
  5. Microsoft Teams: This module provides cmdlets for managing Microsoft Teams, the cloud-based collaboration and communication platform offered by Microsoft as part of Microsoft 365.
  6. Microsoft Graph: This module provides cmdlets for working with Microsoft Graph, which is a RESTful API for accessing data and insights from Microsoft services, including Microsoft 365 and Azure Active Directory.
  7. Microsoft Planner: This module provides cmdlets for managing Microsoft Planner, a task and project management tool offered by Microsoft as part of Microsoft 365.

These modules can be installed from the PowerShell Gallery, the Microsoft Script Center, or other online repositories. Once installed, they provide a rich set of cmdlets for managing and automating tasks in Azure and Microsoft 365.

Basics on getting started with PowerShell:

1.Launch PowerShell: Open PowerShell by searching for “PowerShell” in the Start

menu or by pressing the Windows key + X and selecting “Windows PowerShell”

or “Windows PowerShell (Admin)”.

2. Command Structure: PowerShell commands follow a verb-noun structure. For

example, the command to get the current date is “Get-Date”. The verb indicates

the action, and the noun specifies the target.

3. Getting Help: PowerShell provides extensive help documentation. To get help

for a specific command, use the “Get-Help” cmdlet followed by the command

name. For example, to get help for the “Get-Date” command, run: “Get-Help Get-

Date”.

4. Basic Commands:

  • Displaying Output: Use the “Write-Host” cmdlet to display output on the console. For example: “Write-Host ‘Hello, World!'”.
  • Variables: Use the “$” symbol to create and assign values to variables. For example: “$name = ‘John'”. You can then access the variable by its name.
  • Comments: Use the “#” symbol to add comments in your code. Comments are ignored by PowerShell when executing the script.
  • Running Scripts: Create a text file with a .ps1 extension and write PowerShell commands inside it. Then, run the script by executing its file path. For example: “C:\Scripts\myScript.ps1”.

5. Working with Files and Directories:

  • Listing Files and Directories: Use the “Get-ChildItem” cmdlet to list files and directories in a given path. For example: “Get-ChildItem C:\Folder”.
  • Changing Directory: Use the “Set-Location” cmdlet or the “cd” alias to navigate to a different directory. For example: “Set-Location C:\Folder” or “cd C:\Folder”.
  • Creating a Directory: Use the “New-Item” cmdlet with the “-ItemType Directory” parameter to create a new directory. For example: “New-Item -ItemType Directory -Path C:\NewFolder”.

6. Pipeline and Filtering:

  • Pipeline: PowerShell supports the use of the pipeline operator “|” to pass the output of one command as input to another. For example: “Get-ChildItem | Where-Object {$_.Name -like ‘*.txt’}” lists only the files with a .txt extension.
  • Filtering: Use the “Where-Object” cmdlet to filter objects based on specified criteria. For example: “Get-Process | Where-Object {$_.Name -eq ‘chrome.exe’}” lists only the Chrome processes.

7. Conditional Statements and Loops:

  • If-Else Statement: Use the “if” and “else” statements to execute code based on a condition. For example:

    if ($value -eq 10) { Write-Host “Value is 10” } else { Write-Host “Value is not 10” }
  • For Loop: Use the “for” loop to iterate over a range of values. For example:

    for ($i = 1; $i -le 5; $i++) { Write-Host $i }

8. Exiting PowerShell: To exit PowerShell, simply type “Exit” and press Enter, or

close the PowerShell window.

This is just a basic overview of PowerShell. The language is extensive and offers many more features and cmdlets. You can explore additional commands and concepts by referring to the PowerShell documentation and practicing with different examples.

RELATED POSTS

View all

view all