Windows CMD and PowerShell commands for system administration
List directory contents
dir
Change directory
cd C:\Users\username
Copy files
copy file1.txt file2.txt
Move/rename files
move old.txt new.txt
Delete files
del file.txt
Remove directory
rmdir /s folder
Create directory
mkdir newfolder
Display system information
systeminfo
Show Windows version
ver
Show computer name
hostname
Show current user
whoami
Show network configuration
ipconfig /all
List running processes
tasklist
Terminate process
taskkill /im notepad.exe
Test network connectivity
ping google.com
Trace network route
tracert google.com
Show network statistics
netstat -an
DNS lookup
nslookup google.com
Release IP address
ipconfig /release
Renew IP address
ipconfig /renew
Get available commands
Get-Command *process*
Get command help
Get-Help Get-Process
List processes
Get-Process
List services
Get-Service
List files/directories
Get-ChildItem
Change directory
Set-Location C:\Users
Copy files/directories
Copy-Item file.txt backup.txt
Move/rename items
Move-Item old.txt new.txt
Delete items
Remove-Item file.txt
Create new items
New-Item -ItemType File test.txt
Read file content
Get-Content file.txt
Write to file
Set-Content file.txt "Hello World"
Get computer information
Get-ComputerInfo
List network adapters
Get-NetAdapter
Test network connection
Test-NetConnection google.com
Get WMI objects
Get-WmiObject -Class Win32_OperatingSystem
Start a process
Start-Process notepad
Stop a process
Stop-Process -Name notepad
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
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)"
}
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
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 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
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