PowerShell: A learning guide for beginners to advanced

Kushal Pokhrel
4 min readSep 9, 2023

--

PowerShell is a versatile and powerful platform command-line shell and scripting language developed by Microsoft. It is designed for automating administrative tasks and configuration management on Windows, but it is now available on various platforms, including macOS and Linux. In this beginner’s guide, we will cover the basics of PowerShell, what it is used for, and how to start using it.

Powershell -Logo

What is PowerShell?

PowerShell is a command-line shell that enables users to interact with their computer systems using text-based commands. It provides access to a wide range of system management and automation tasks. Unlike traditional command shells like Command Prompt, PowerShell uses a consistent and intuitive syntax based on verb-noun pairs, making it easier to learn and use.

What Can You Use PowerShell For?

PowerShell is a versatile tool with a wide range of applications, including:

  1. Automation: PowerShell is excellent for automating repetitive tasks. You can write scripts to perform tasks such as file management, system configuration, and software installation.
  2. System Administration: System administrators use PowerShell to manage and configure Windows servers and workstations. You can perform tasks like user management, network configuration, and system monitoring.
  3. Scripting: PowerShell is a full-featured scripting language. You can write scripts to automate complex processes, making it a valuable tool for developers and IT professionals.
  4. Reporting: PowerShell can retrieve and process data from various sources, including logs and databases, to generate reports and perform data analysis.
  5. Cloud Computing: PowerShell integrates with cloud platforms like Microsoft Azure and AWS, allowing you to manage cloud resources and automate cloud-related tasks.
  6. Active Directory Management: PowerShell is particularly powerful for managing Active Directory environments, making it essential for Windows domain administrators.

Getting Started with PowerShell

Installation:

If you’re using Windows, PowerShell is pre-installed. However, if you’re on macOS or Linux, you can install PowerShell Core (also known as PowerShell 7) from the official GitHub repository: PowerShell GitHub Releases.

Opening PowerShell:

  • Windows: Press Win + X and select "Windows Terminal" or "Command Prompt" to open a PowerShell window.
  • macOS/Linux: Open your terminal emulator and type pwsh to launch PowerShell.

Beginner Stage:

1. Writing Your First PowerShell Script (Beginner):

Objective: Display a greeting message on the screen.

powershell
Write-Host "Hello, PowerShell!"

This simple script uses the Write-Host cmdlet to display a greeting message when executed.

2. Basic File Operations (Beginner):

Objective: Create, list, and remove files and directories.

powershell
# Create a new text file
New-Item -Path "example.txt" -ItemType "file"
# List files in the current directory
Get-ChildItem
# Remove the text file
Remove-Item -Path "example.txt"

These commands demonstrate creating a file, listing files in a directory, and removing a file using PowerShell.

Moderate Stage:

1. User Management (Moderate):

Objective: Create, modify, and delete user accounts.

powershell
# Create a new user
New-LocalUser -Name "NewUser" -Password "P@ssw0rd" -Description "New User Account"
# Change the user's password
Set-LocalUser -Name "NewUser" -Password "NewP@ssw0rd"
# Disable the user account
Disable-LocalUser -Name "NewUser"
# Remove the user account
Remove-LocalUser -Name "NewUser"

These commands demonstrate user management tasks like creating, modifying, disabling, and removing user accounts on a local system.

2. Registry Operations (Moderate):

Objective: Read and modify Windows Registry keys.

powershell
# Read a registry key value
(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Google Chrome").DisplayName
# Create a new registry key
New-ItemProperty -Path "HKCU:\Software\MyApp" -Name "Setting" -Value "Value123"
# Modify an existing registry key
Set-ItemProperty -Path "HKCU:\Software\MyApp" -Name "Setting" -Value "NewValue456"
# Delete a registry key
Remove-Item -Path "HKCU:\Software\MyApp" -Recurse

These commands illustrate how to interact with the Windows Registry, including reading, creating, modifying, and deleting keys and values.

Advanced Stage:

1. Automating Azure Resource Deployment (Advanced):

Objective: Use PowerShell to deploy Azure resources.

powershell
# Login to Azure
Connect-AzAccount
# Create a resource group
New-AzResourceGroup -Name "MyResourceGroup" -Location "East US"
# Create a virtual machine
New-AzVM -ResourceGroupName "MyResourceGroup" -Name "MyVM" -Image "UbuntuLTS" -AdminUsername "adminuser" -AdminPassword "P@ssw0rd" -Size "Standard_B1s"
# Remove the resource group and all its resources
Remove-AzResourceGroup -Name "MyResourceGroup" -Force

In this example, we use PowerShell to interact with Microsoft Azure, including logging in, creating a resource group, deploying a virtual machine, and cleaning up resources.

2. Advanced Scripting (Advanced):

Objective: Create a script that automates a complex task, such as data migration.

powershell
# Define source and destination paths
$sourcePath = "C:\SourceData"
$destinationPath = "D:\Backup"
# Copy files and subdirectories recursively
Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse
# Log the completion of the task
Add-Content -Path "C:\Logs\migration.log" -Value "$(Get-Date): Data migration completed."

This advanced script showcases complex automation, including copying data recursively and logging the task’s completion.

Conclusion:

Remember that as you progress from beginner to advanced levels in PowerShell, your scripts will become more sophisticated, allowing you to automate intricate tasks, manage cloud resources, and perform system administration efficiently. Continuously practice and explore PowerShell’s extensive capabilities to become a proficient user. At this stage you already have a clue on what's going on with your learning journey, and if you need additional resources. Microsoft Learn has great content and courses on teaching Powershell to the public.

References

GitHub. (n.d.). Releases · PowerShell/PowerShell. [online] Available at: https://github.com/PowerShell/PowerShell/releases [Accessed 9 Sep. 2023].

‌softchris (n.d.). Introduction to PowerShell — Training. [online] learn.microsoft.com. Available at: https://learn.microsoft.com/en-us/training/modules/introduction-to-powershell/.

‌sdwheeler (n.d.). PowerShell Documentation — PowerShell. [online] learn.microsoft.com. Available at: https://learn.microsoft.com/en-us/powershell/.

--

--

Kushal Pokhrel

M-Shaped IT Professional • Google Developer Expert, Machine Learning • Sessional Lecturer • AI/ML Researcher