CLI: Basics

Know your way around Windows and Linux terminals


Command line terminals open a world possibilites and efficiency. They allow for close interaction with the operating system which is imperative for troubleshooting, automation, remote management, and more. Below are some basic commands, color coded for Windows and Linux and specific for Command Prompt, PowerShell, and Bash.
  • Elevate to Administrator/Root
    • powershell -Command "Start-Process cmd -Verb RunAs"
    • Start-Process PowerShell -Verb RunAs
    • sudo su

Terminal navigation
  • Clear terminal
    • cls
    • clear
    • clear

  • Where am i?
    • cd
    • Get-Location
    • pwd

  • Enter directory
    • cd c:\users\massi
    • cd /home/massi

  • Go back directory
    • cd ..

  • Move to C drive
    • c: cd c: user

  • Show files/folders in directory
    • dir /b
    • Get-ChildItem -Name
    • ls -1

  • Search system for exact file name
    • dir /s /b C:\file.txt
    • Get-ChildItem -Path C:\ -Recurse -File -Filter "file.txt"
    • find / -type f -name "file.txt"

  • Show folder tree
    • tree
    • tree /f


File/Folder operations
  • Create directory
    • mkdir

  • Delete directory
    • rd /s c:\path\to\directory
    • Remove-Item -Path "c:\path\to\directory" -Recurse
    • rm -r /path/to/directory

  • Create file
    • echo hello world > file.txt
    • "hello world" | Out-File -FilePath "file.txt"
    • hello world > file.txt

  • View file
    • more file.txt
    • cat file.txt

  • Move file
    • move "C:\Users\file.txt" "C:\path\to\destination"
    • Move-Item "C:\Users\file.txt" "C:\path\to\destination\"
    • mv /path/to/file.txt /destination/file.txt

  • Move folder
    • move "C:\source\folder" "C:\path\to\destination"
    • Move-Item -Path "C:\source\directory" -Destination "C:\destination\directory"
    • mv /path/to/source_directory /path/to/destination/

  • Rename file
    • ren file.txt file2.txt
    • Rename-Item -Path "file.txt" -NewName "file2.txt"
    • mv file.txt file2.txt

  • Delete file
    • del file.txt
    • rm file.txt

  • Copy file
    • copy file.txt C:\path\to\destination
    • rsync --progress source_file.txt /destination/folder

  • Copy folder
    • xcopy "C:\source\folder" "C:\destination\folder" /E /H /C /K /Y
    • Copy-Item -Path "C:\source\folder" -Destination "C:\destination\folder" -Recurse -Force
    • sudo rsync -avz --progress /mnt/source_foler /home/user/destination_folder


Device information
  • General
    • systeminfo

  • Get serial number
    • wmic bios get serialnumber
    • (gwmi Win32_BIOS).SerialNumber
    • sudo dmidecode -t bios

  • Get product name
    • wmic csproduct get name
    • (gwmi Win32_ComputerSystemProduct).Name
    • sudo dmidecode -s system-product-name

  • Get model type
    • wmic csproduct get version
    • (gwmi Win32_ComputerSystemProduct).Version
    • lsb_release -a

  • Get MAC addresses
    • ipconfig /all | findstr /C:"Description" /C:"Physical Address"

  • Uptime
    • systeminfo | find "System Boot Time"
    • uptime


Networking
  • Get local IP
    • ipconfig | findstr /i "IPv4 Address"
    • (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter ‘ipenabled = “true”’).IPAddress
    • ip address | awk '/inet / {print $2}'

  • Get public IP
    • curl ifconfig.me
    • (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
    • curl ifconfig.me

  • Full network reset
    • netsh winsock reset
    • netsh int ip reset
    • ipconfig /release
    • ipconfig /renew
    • ipconfig /flushdns

  • Show network interfaces
    • ipconfig /all
    • ifconfig

  • Traceroute
    • tracert google.com
    • traceroute google.com


Users
  • Enable default Administrator account
    • net user administrator /active:yes

  • View user accounts
    • net users
    • Get-LocalUser
    • getent passwd | cut -d: -f1

  • Add user
    • net user user2 password2 /add
    • New-LocalUser -Name "user2" -Password (ConvertTo-SecureString "password2" -AsPlainText -Force)
    • sudo useradd user2

  • Delete user
    • sudo userdel user2
    • Remove-LocalUser -Name "user2"
    • sudo userdel -r user2


Troubleshooting
  • Repair corrupt system files
    • sfc /scannow
    • dism /Online /Cleanup-Image /RestoreHealth

  • Generate battery report
    • powercfg /batteryreport
    • acpi > battery_report.txt

  • Restart into BIOS
    • shutdown /r /fw /t 0


Keys and passwords
  • Get BitLocker key
    • manage-bde -protectors C: -get

  • Get Windows product key
    • wmic path SoftwareLicensingService get OA3xOriginalProductKey

  • Set date/time
    • time
    • Set-Date -Date "YYYY-MM-DD"
    • Set-Date -Time "HH:mm:ss"
    • date
    • cal

  • Show processes
    • tasklist
    • -e

  • Show space on disk
    • fsutil volume diskfree c:
    • df -h

  • Log off
    • logoff
    • logout