PowerShell Commands : Quick Guide


πŸ”₯ What is PowerShell?

PowerShell is a task automation and configuration management framework developed by Microsoft. It consists of two core components:

  • A command-line shell that allows you to execute commands directly.
  • A scripting language that enables the automation of complex administrative tasks.

PowerShell is built on the .NET framework and supports object-oriented programming, making it far more powerful than traditional command-line tools like CMD. With PowerShell, you can manage local and remote systems, perform bulk operations, and automate repetitive tasks effortlessly.

Unlike other shell environments, PowerShell uses cmdlets (pronounced command-lets)β€”lightweight commands that interact with the system and return objects rather than text. This object-based approach makes it easier to filter, sort, and manipulate data, providing greater flexibility and efficiency.


βš™οΈ Why Do We Need PowerShell?

In today’s IT-driven world, managing large infrastructures manually is both time-consuming and error-prone. PowerShell offers automation and efficiency, which is crucial for IT administrators, DevOps engineers, and developers. Here’s why PowerShell is essential:

  • Automates Repetitive Tasks:
    Instead of manually creating hundreds of user accounts or performing system audits, PowerShell scripts can automate these tasks, saving time and ensuring consistency.
  • Streamlines System Management:
    With PowerShell, you can remotely manage multiple machines simultaneously. This is particularly useful in enterprise environments with hundreds or thousands of systems.
  • Improves Productivity with Bulk Operations:
    PowerShell allows you to perform bulk operations such as managing Active Directory (AD) users, assigning permissions, or exporting data to CSV files with just a few lines of code.
  • Seamless Integration with Cloud Platforms:
    PowerShell integrates natively with Azure, enabling you to automate cloud resource management, deploy infrastructure as code (IaC), and manage virtual machines effortlessly.
  • Reduces Human Error:
    By automating processes, PowerShell reduces the risk of manual errors, ensuring consistent results and improving reliability.

πŸ‘₯ Who Uses PowerShell?

PowerShell is widely used across various IT roles, including:

  • Security Professionals:
    PowerShell is used in cybersecurity for penetration testing, log analysis, and automating security audits.
  • System Administrators:
    IT admins leverage PowerShell to manage servers, configure services, create scripts for backup automation, and monitor system performance. Its ability to handle bulk operations makes it an essential tool for infrastructure management.
  • Cloud Engineers and DevOps Teams:
    Cloud engineers use PowerShell to automate Azure and AWS resource management, implement CI/CD pipelines, and manage cloud services programmatically. PowerShell is also integral in Infrastructure as Code (IaC) practices.
  • Developers:
    Software engineers use PowerShell to automate build processes, manage application deployments, and integrate with APIs. It is particularly useful in continuous integration and deployment workflows.
  • SharePoint Administrators:
    SharePoint admins heavily rely on PowerShell for site management, data migrations, and permission handling. With PnP PowerShell, they can automate complex SharePoint operations.

πŸ› οΈ Applications and Platforms Using PowerShell

PowerShell is integrated into or supports various applications, including:

  • Windows OS:
    PowerShell is the default shell in modern Windows systems, allowing you to manage local and remote Windows environments.
  • Azure and Microsoft 365:
    PowerShell provides a robust way to interact with Azure resources, automate management tasks, and configure Microsoft 365 services.
  • SharePoint:
    SharePoint administrators use PnP PowerShell to automate site creation, manage document libraries, handle permissions, and perform audits.
  • Active Directory (AD):
    PowerShell simplifies the management of Active Directory objects such as users, groups, and policies. It enables bulk modifications and detailed audits.
  • Exchange Server and Office 365:
    Administrators use PowerShell to manage mailboxes, perform email migrations, and automate recurring tasks in Exchange Server and Office 365 environments.
  • SQL Server:
    PowerShell supports SQL Server Management tasks, such as executing queries, managing backups, and automating database maintenance.
  • Docker and Kubernetes:
    PowerShell can interact with Docker containers and Kubernetes clusters, making it a valuable tool for containerized application management.

🚦 Prerequisites for Using PowerShell

Before diving into the cheat sheet, you need the following:

βœ… Installation
  • Windows: PowerShell is pre-installed in Windows 10 and later versions.
  • Linux and macOS: Install PowerShell Core from Microsoft’s GitHub or via package managers like apt or brew.
βœ… Modules

Install necessary modules to extend functionality:

Install-Module -Name PowerShellGet -Force -AllowClobber  
Install-Module -Name Az -Repository PSGallery -Force  
βœ… Execution Policy

Enable script execution by changing the policy:

Set-ExecutionPolicy RemoteSigned -Scope Process

πŸ“ PowerShell Command Cheat Sheet
πŸ› οΈ Basic Commands
# Display PowerShell version  
$PSVersionTable.PSVersion  

# List available commands  
Get-Command  

# Get help for a command  
Get-Help Get-Process
πŸ“‚ File and Folder Operations
# Create a new file  
New-Item -ItemType File -Path "C:\Temp\test.txt"  

# Copy a file  
Copy-Item -Path "C:\Temp\test.txt" -Destination "C:\Backup\"  

# Delete a file  
Remove-Item -Path "C:\Temp\test.txt"  
πŸ”Ž System Information
# Get OS details  
Get-WmiObject -Class Win32_OperatingSystem  

# Get system uptime  
(Get-CimInstance Win32_OperatingSystem).LastBootUpTime 
πŸ” User and Permission Management
# Get all local users  
Get-LocalUser  

# Add a new local user  
New-LocalUser -Name "JohnDoe" -Password (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force)  

# Assign permissions  
Add-NTFSAccess -Path "C:\Data" -Account "Domain\User" -AccessRights FullControl
🌐 Network Management
# Get IP configuration  
Get-NetIPAddress  

# Test network connection  
Test-NetConnection google.com -TraceRoute  

# Get all network adapters  
Get-NetAdapter
πŸ’Ύ Processes and Services
# List all processes  
Get-Process  

# Stop a process  
Stop-Process -Name "notepad"  

# List all services  
Get-Service  
πŸ› οΈ PowerShell for SharePoint
# Connect to SharePoint Online  
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com" -Interactive

# List all document libraries
Get-PnPList | Where-Object {$_.BaseType -eq "DocumentLibrary"}

# Create a new site
New-PnPSite -Type TeamSite -Title "Demo Site" -Alias "DemoSite" -Owner "user@tenant.onmicrosoft.com"

βœ… Best Practices for PowerShell

Use Meaningful Variables:

  • Avoid using single-letter variables for readability.Example:

$userList = Get-ADUser -Filter *

Error Handling:

  • Use Try-Catch blocks for robust scripts.

Try { Get-Content "C:\InvalidPath.txt" } Catch { Write-Host "File not found" }

Comment Your Code:

  • Add meaningful comments to increase readability.

# This script lists all services Get-Service

Use Modules:

Leverage pre-built modules for efficiency.


βš–οΈ Pros and Cons of PowerShell
βœ… Pros
  • Powerful automation capabilities.
  • Integration with various Microsoft products.
  • Supports remote management.
  • Large community and extensive documentation.
❌ Cons
  • Windows-centric (though PowerShell Core supports Linux/macOS).
  • Steep learning curve for complex scripting.
  • Can be dangerous when running destructive commands.

πŸ”₯ Use Cases and Sample Implementations
πŸ”Ή Automating User Creation
$users = Import-Csv "C:\Users.csv"  
foreach ($user in $users) {
New-LocalUser -Name $user.Name -Password (ConvertTo-SecureString $user.Password -AsPlainText -Force)
}
πŸ”Ή SharePoint Document Library Audit
Connect-PnPOnline -Url "https://tenant.sharepoint.com" -Interactive  
$docs = Get-PnPListItem -List "Documents"
$docs | Export-Csv -Path "C:\Audit.csv" -NoTypeInformation

πŸ” Comparison with Alternatives
FeaturePowerShellCommand Prompt (CMD)Bash (Linux)
PlatformWindows, Linux, MacWindows onlyLinux, Mac
Complex ScriptingYesNoYes
Object-OrientedYesNoNo
Modules SupportExtensiveLimitedExtensive
Remote ManagementYesNoYes

πŸ”— References

PowerShell is an essential tool for IT professionals and developers, offering a robust platform for automation, scripting, and system management. By mastering PowerShell commands and best practices, you can significantly enhance your productivity and efficiency in managing infrastructure and applications.

Let me know if you’d like more sample scripts or detailed tutorials on specific PowerShell use cases! πŸš€


Authentication Automation Backup Batching Compliance Content Type CSS Extensions Flows Google GULP Javascript Limitations Metadata MFA Microsoft Node NodeJs O365 OneDrive Permissions PnP PnPJS Policy Power Automate PowerAutomate PowerShell React ReactJs Rest API Rest Endpoint Send an HTTP Request to SharePoint SharePoint SharePoint List SharePoint Modern SharePoint Online Sharing Is Caring SPFX SPO Sync Tags Teams Termstore Versioning WebParts

Leave a Comment

Your email address will not be published. Required fields are marked *