Windows Command Prompt

Windows CMD and PowerShell commands for system administration

CMD - File Management

dir

List directory contents

dir

cd

Change directory

cd C:\Users\username

copy

Copy files

copy file1.txt file2.txt

move

Move/rename files

move old.txt new.txt

del

Delete files

del file.txt

rmdir

Remove directory

rmdir /s folder

mkdir

Create directory

mkdir newfolder

CMD - System Information

systeminfo

Display system information

systeminfo

ver

Show Windows version

ver

hostname

Show computer name

hostname

whoami

Show current user

whoami

ipconfig

Show network configuration

ipconfig /all

tasklist

List running processes

tasklist

taskkill

Terminate process

taskkill /im notepad.exe

CMD - Network Commands

ping

Test network connectivity

ping google.com

tracert

Trace network route

tracert google.com

netstat

Show network statistics

netstat -an

nslookup

DNS lookup

nslookup google.com

ipconfig /release

Release IP address

ipconfig /release

ipconfig /renew

Renew IP address

ipconfig /renew

PowerShell - PowerShell Basics

Get-Command

Get available commands

Get-Command *process*

Get-Help

Get command help

Get-Help Get-Process

Get-Process

List processes

Get-Process

Get-Service

List services

Get-Service

Get-ChildItem

List files/directories

Get-ChildItem

Set-Location

Change directory

Set-Location C:\Users

PowerShell - PowerShell File Operations

Copy-Item

Copy files/directories

Copy-Item file.txt backup.txt

Move-Item

Move/rename items

Move-Item old.txt new.txt

Remove-Item

Delete items

Remove-Item file.txt

New-Item

Create new items

New-Item -ItemType File test.txt

Get-Content

Read file content

Get-Content file.txt

Set-Content

Write to file

Set-Content file.txt "Hello World"

PowerShell - PowerShell System Management

Get-ComputerInfo

Get computer information

Get-ComputerInfo

Get-NetAdapter

List network adapters

Get-NetAdapter

Test-NetConnection

Test network connection

Test-NetConnection google.com

Get-WmiObject

Get WMI objects

Get-WmiObject -Class Win32_OperatingSystem

Start-Process

Start a process

Start-Process notepad

Stop-Process

Stop a process

Stop-Process -Name notepad

Common Patterns

Batch File Basics

Creating and running batch files

@echo off
REM This is a comment
echo Hello World
pause

REM Variables
set name=John
echo Hello %name%

REM Conditional statements
if exist file.txt (
    echo File exists
) else (
    echo File does not exist
)

REM Loops
for %%i in (1 2 3 4 5) do (
    echo Number: %%i
)

REM Functions
:myfunction
echo This is a function
goto :eof

call :myfunction

PowerShell Script Basics

Creating and running PowerShell scripts

# PowerShell script example
param(
    [string]$Name = "World"
)

# Variables
$message = "Hello $Name"
Write-Host $message

# Functions
function Get-Greeting {
    param([string]$Name)
    return "Hello, $Name!"
}

# Conditional statements
if (Test-Path "file.txt") {
    Write-Host "File exists"
} else {
    Write-Host "File does not exist"
}

# Loops
for ($i = 1; $i -le 5; $i++) {
    Write-Host "Number: $i"
}

# Arrays
$fruits = @("Apple", "Banana", "Orange")
foreach ($fruit in $fruits) {
    Write-Host "Fruit: $fruit"
}

# Error handling
try {
    Get-Content "nonexistent.txt"
} catch {
    Write-Host "Error: $($_.Exception.Message)"
}

System Administration Tasks

Common administrative tasks

# Check disk space
Get-WmiObject -Class Win32_LogicalDisk | Select-Object DeviceID, @{Name="Size(GB)";Expression={[math]::Round($_.Size/1GB,2)}}, @{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB,2)}}

# List installed programs
Get-WmiObject -Class Win32_Product | Select-Object Name, Version

# Check Windows updates
Get-HotFix | Sort-Object -Property InstalledOn -Descending | Select-Object -First 10

# Monitor system performance
Get-Counter "Processor(_Total)% Processor Time", "MemoryAvailable MBytes" -SampleInterval 5 -MaxSamples 10

# Check event logs
Get-EventLog -LogName Application -Newest 10 | Select-Object TimeGenerated, Source, Message

# Network diagnostics
Test-NetConnection -ComputerName google.com -Port 80
Test-NetConnection -ComputerName 8.8.8.8 -Port 53

File Management Scripts

Advanced file operations

# Find files by extension
Get-ChildItem -Path C:\ -Recurse -Include "*.txt" -ErrorAction SilentlyContinue

# Backup files
$source = "C:\Important"
$destination = "D:\Backup\$(Get-Date -Format 'yyyy-MM-dd')"
Copy-Item -Path $source -Destination $destination -Recurse

# Clean up old files
$cutoffDate = (Get-Date).AddDays(-30)
Get-ChildItem -Path "C:\Temp" -Recurse | Where-Object {$_.LastWriteTime -lt $cutoffDate} | Remove-Item -Force

# Monitor file changes
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Monitor"
$watcher.Filter = "*.txt"
$watcher.EnableRaisingEvents = $true

Register-ObjectEvent $watcher "Changed" -Action {
    Write-Host "File changed: $($Event.SourceEventArgs.FullPath)"
}

Network Troubleshooting

Network diagnostic commands

# Check network connectivity
ping google.com
Test-NetConnection google.com

# Check DNS resolution
nslookup google.com
Resolve-DnsName google.com

# Check network route
tracert google.com
Test-NetConnection google.com -TraceRoute

# Check open ports
netstat -an | findstr :80
Get-NetTCPConnection -LocalPort 80

# Check network adapter status
ipconfig /all
Get-NetAdapter | Where-Object {$_.Status -eq "Up"}

# Reset network configuration
ipconfig /release
ipconfig /renew
ipconfig /flushdns

# Check firewall status
netsh advfirewall show allprofiles
Get-NetFirewallProfile

Tips & Best Practices

Use PowerShell for complex tasks, CMD for simple file operations

Always run PowerShell as Administrator for system-level operations

Use -ErrorAction SilentlyContinue to suppress errors in PowerShell

Use Get-Help to learn about PowerShell cmdlets

Use Set-ExecutionPolicy to control script execution