IT Scripts & Resources

A collection of scripts, tools, and knowledge to help you work smarter. Copy, adapt, and use freely.

PowerShellPowerShell

Bulk User Account Creator

Creates multiple Active Directory user accounts from a CSV file. Handles password generation, group assignment, and email notification.

# Bulk AD User Creator
# Usage: .\Create-ADUsers.ps1 -CsvPath .\users.csv

param(
    [Parameter(Mandatory=$true)]
    [string]$CsvPath
)

Import-Module ActiveDirectory

$users = Import-Csv -Path $CsvPath

foreach ($user in $users) {
    $password = ConvertTo-SecureString "TempPass@123" -AsPlainText -Force
    
    New-ADUser `
        -Name "$($user.FirstName) $($user.LastName)" `
        -GivenName $user.FirstName `
        -Surname $user.LastName `
        -SamAccountName $user.Username `
        -UserPrincipalName "$($user.Username)@yourdomain.com" `
        -Department $user.Department `
        -AccountPassword $password `
        -Enabled $true `
        -ChangePasswordAtLogon $true
    
    Write-Host "Created user: $($user.Username)" -ForegroundColor Green
}

Write-Host "Done. $($users.Count) users created." -ForegroundColor Cyan
PowerShellPowerShell

System Health Check

Generates a quick health report for a Windows machine: CPU, memory, disk usage, running services, and recent event log errors.

# System Health Check
# Run as Administrator for full output

Write-Host "=== SYSTEM HEALTH REPORT ==" -ForegroundColor Cyan
Write-Host "Generated: $(Get-Date)" -ForegroundColor Gray

# CPU Usage
$cpu = Get-WmiObject Win32_Processor | Measure-Object -Property LoadPercentage -Average
Write-Host "`nCPU Usage: $($cpu.Average)%" -ForegroundColor $(if ($cpu.Average -gt 80) { 'Red' } else { 'Green' })

# Memory
$os = Get-WmiObject Win32_OperatingSystem
$memUsed = [math]::Round(($os.TotalVisibleMemorySize - $os.FreePhysicalMemory) / 1MB, 2)
$memTotal = [math]::Round($os.TotalVisibleMemorySize / 1MB, 2)
Write-Host "Memory: $memUsed GB / $memTotal GB" -ForegroundColor $(if ($memUsed/$memTotal -gt 0.9) { 'Red' } else { 'Green' })

# Disk Usage
Get-PSDrive -PSProvider FileSystem | ForEach-Object {
    $pct = [math]::Round(($_.Used / ($_.Used + $_.Free)) * 100, 1)
    Write-Host "Disk $($_.Name): $pct% used" -ForegroundColor $(if ($pct -gt 85) { 'Red' } else { 'Green' })
}

# Recent Errors (last 24h)
$errors = Get-EventLog -LogName System -EntryType Error -Newest 5 -ErrorAction SilentlyContinue
Write-Host "`nRecent System Errors (last 5):"
$errors | Select-Object TimeGenerated, Source, Message | Format-Table -AutoSize
BashBash / Shell

Server Backup Script

Backs up specified directories to a timestamped archive, rotates old backups, and logs the result. Suitable for cron scheduling.

#!/bin/bash
# Server Backup Script
# Add to cron: 0 2 * * * /opt/scripts/backup.sh

BACKUP_DIR="/backups"
SOURCE_DIRS=("/etc" "/var/www" "/home")
RETENTION_DAYS=7
LOG_FILE="/var/log/backup.log"
DATE=$(date +%Y%m%d_%H%M%S)

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

mkdir -p "$BACKUP_DIR"
log "Starting backup..."

for dir in "${SOURCE_DIRS[@]}"; do
    if [ -d "$dir" ]; then
        ARCHIVE="$BACKUP_DIR/$(basename $dir)_$DATE.tar.gz"
        tar -czf "$ARCHIVE" "$dir" 2>/dev/null
        log "Backed up $dir -> $ARCHIVE ($(du -sh $ARCHIVE | cut -f1))"
    else
        log "WARNING: $dir not found, skipping"
    fi
done

# Rotate old backups
find "$BACKUP_DIR" -name "*.tar.gz" -mtime +$RETENTION_DAYS -delete
log "Cleaned backups older than $RETENTION_DAYS days"
log "Backup complete."
BashNetworking

Network Port Scanner

Scans common ports on a target host and reports open/closed status. Useful for quick network audits without installing nmap.

#!/bin/bash
# Quick Port Scanner (no nmap required)
# Usage: ./portscan.sh 192.168.1.1

HOST=${1:-"localhost"}
COMMON_PORTS=(21 22 23 25 53 80 110 143 443 445 3306 3389 5432 6379 8080 8443)
TIMEOUT=1

echo "Scanning $HOST..."
echo "----------------------------"

for port in "${COMMON_PORTS[@]}"; do
    (echo >/dev/tcp/$HOST/$port) 2>/dev/null
    if [ $? -eq 0 ]; then
        case $port in
            21)  svc="FTP" ;;
            22)  svc="SSH" ;;
            23)  svc="Telnet" ;;
            25)  svc="SMTP" ;;
            53)  svc="DNS" ;;
            80)  svc="HTTP" ;;
            443) svc="HTTPS" ;;
            3306) svc="MySQL" ;;
            3389) svc="RDP" ;;
            *)   svc="Unknown" ;;
        esac
        echo "  OPEN   $port/tcp  ($svc)"
    fi
done

echo "----------------------------"
echo "Scan complete."
PowerShellSecurity

Windows Security Audit

Checks common Windows security settings: firewall status, Windows Defender, auto-updates, open shares, and local admin accounts.

# Windows Security Audit
# Run as Administrator

Write-Host "=== WINDOWS SECURITY AUDIT ===" -ForegroundColor Cyan

# Firewall Status
$fw = Get-NetFirewallProfile
$fw | ForEach-Object {
    $color = if ($_.Enabled) { 'Green' } else { 'Red' }
    Write-Host "Firewall [$($_.Name)]: $(if ($_.Enabled) {'ON'} else {'OFF'})" -ForegroundColor $color
}

# Windows Defender
$defender = Get-MpComputerStatus -ErrorAction SilentlyContinue
if ($defender) {
    Write-Host "`nDefender Real-Time: $(if ($defender.RealTimeProtectionEnabled) {'ON'} else {'OFF'})" -ForegroundColor $(if ($defender.RealTimeProtectionEnabled) {'Green'} else {'Red'})
    Write-Host "Definitions Age: $($defender.AntivirusSignatureAge) days" -ForegroundColor $(if ($defender.AntivirusSignatureAge -gt 3) {'Yellow'} else {'Green'})
}

# Auto-Update
$au = (New-Object -ComObject Microsoft.Update.AutoUpdate).Settings
Write-Host "`nAuto Updates: $(if ($au.NotificationLevel -eq 4) {'Enabled'} else {'Check Settings'})" -ForegroundColor $(if ($au.NotificationLevel -eq 4) {'Green'} else {'Yellow'})

# Local Admins
Write-Host "`nLocal Administrators:"
Get-LocalGroupMember -Group "Administrators" | Select-Object Name, PrincipalSource | Format-Table -AutoSize

# Open Shares
Write-Host "Network Shares:"
Get-SmbShare | Where-Object { $_.Name -notlike '*$' } | Select-Object Name, Path | Format-Table -AutoSize
PowerShellPowerShell

Disk Cleanup Automation

Cleans temp files, Windows Update cache, recycle bin, and old log files. Safe to run on any Windows machine.

# Disk Cleanup Automation
# Run as Administrator

$before = (Get-PSDrive C).Free
Write-Host "Free space before: $([math]::Round($before/1GB, 2)) GB" -ForegroundColor Yellow

# Temp files
$tempPaths = @(
    $env:TEMP,
    "C:\Windows\Temp",
    "C:\Windows\SoftwareDistribution\Download"
)

foreach ($path in $tempPaths) {
    if (Test-Path $path) {
        Get-ChildItem -Path $path -Recurse -Force -ErrorAction SilentlyContinue |
            Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
        Write-Host "Cleaned: $path" -ForegroundColor Green
    }
}

# Recycle Bin
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Write-Host "Recycle Bin emptied" -ForegroundColor Green

# Old log files (>30 days)
Get-ChildItem "C:\Windows\Logs" -Recurse -Filter "*.log" -ErrorAction SilentlyContinue |
    Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) } |
    Remove-Item -Force -ErrorAction SilentlyContinue
Write-Host "Old logs removed" -ForegroundColor Green

$after = (Get-PSDrive C).Free
$freed = [math]::Round(($after - $before)/1MB, 2)
Write-Host "`nFreed: $freed MB" -ForegroundColor Cyan

Script Categories

Copyright © 2025 Get IT Dunn Right - All Rights Reserved.