Skip to content

NaNPacket/Powershell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 

Repository files navigation

πŸš€ PowerShell Quick Reference Guide

PowerShell Logo

Made with Love PowerShell Platform

πŸ“‹ Table of Contents

🎯 Basic Commands

System Information

# Get System Information
Get-ComputerInfo

# Get PowerShell Version
$PSVersionTable

# Get Current Location
Get-Location    # or pwd

Help System

# Get help for a command
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Help Get-Process -Detailed
Get-Help Get-Process -Full

# Update help documentation
Update-Help

πŸ“‚ File System Operations

Directory Operations

# Navigate directories
Set-Location path\to\directory    # or cd
Get-Location                      # or pwd

# Create/Remove directories
New-Item -ItemType Directory -Path "NewFolder"
Remove-Item -Path "FolderToDelete" -Recurse

# List items
Get-ChildItem    # or dir or ls
Get-ChildItem -Path C:\ -Recurse -Filter *.txt

File Operations

# Create/Delete files
New-Item -ItemType File -Path "test.txt"
Remove-Item "file.txt"

# Copy/Move files
Copy-Item "source.txt" -Destination "dest.txt"
Move-Item "old.txt" "new.txt"

# Read/Write files
Get-Content "file.txt"
Set-Content "file.txt" "New content"
Add-Content "file.txt" "Appended content"

πŸ”„ Process Management

# List all processes
Get-Process

# Start a new process
Start-Process notepad.exe

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

# Get service status
Get-Service

🌐 Network Operations

# Test network connection
Test-NetConnection google.com
Test-NetConnection -ComputerName server01 -Port 80

# Get IP configuration
Get-NetIPConfiguration
Get-NetAdapter

# Download file from internet
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"

πŸ“Š Variables and Data Types

# Variable declaration
$name = "John"
$age = 30
$isActive = $true
$numbers = @(1, 2, 3, 4, 5)
$hash = @{
    Key1 = "Value1"
    Key2 = "Value2"
}

# Type casting
[int]"123"
[string]256
[datetime]"2024-01-01"

πŸ”€ Flow Control

Conditionals

# If statement
if ($age -gt 18) {
    "Adult"
} elseif ($age -eq 18) {
    "Just turned adult"
} else {
    "Minor"
}

# Switch statement
switch ($value) {
    1 { "One" }
    2 { "Two" }
    default { "Other" }
}

Loops

# ForEach loop
foreach ($item in $collection) {
    $item
}

# For loop
for ($i = 0; $i -lt 5; $i++) {
    "Iteration $i"
}

# While loop
while ($condition) {
    # code
}

πŸ“¦ Functions and Modules

# Function definition
function Get-Square {
    param(
        [Parameter(Mandatory=$true)]
        [int]$Number
    )
    return $Number * $Number
}

# Module operations
Get-Module -ListAvailable
Import-Module ModuleName
Get-Command -Module ModuleName

⚠️ Error Handling

# Try-Catch block
try {
    # Risky operation
    $result = 1/0
} catch {
    Write-Error "An error occurred: $_"
} finally {
    # Cleanup code
}

# Error preference
$ErrorActionPreference = "Stop"    # or "Continue", "SilentlyContinue"

πŸ’‘ Best Practices

Code Style

  • Use approved verbs for function names (Get-, Set-, New-, Remove-, etc.)
  • Follow consistent naming conventions (PascalCase for functions, camelCase for variables)
  • Add comments for complex operations
  • Use proper indentation and spacing

Performance

  • Use pipeline operations when possible
  • Avoid unnecessary type conversions
  • Use proper filtering at the source
  • Consider using background jobs for long-running operations

Security

  • Always validate input
  • Use secure string for sensitive data
  • Follow principle of least privilege
  • Implement proper error handling

πŸ“š Additional Resources

πŸ“„ License

This quick reference guide is released under the MIT License. Feel free to use, modify, and distribute as needed.


Made with ❀️ by Selam

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published