Windows Command Line: File System & Navigation

🏴‍☠️ Becoming a Cap10 of the Command Line: Windows File System & Navigation

Objective: Teach absolute beginners how to confidently navigate and manage files in Windows using the command line (Command Prompt and PowerShell).


Part 1: Understanding the Windows File System

Before we dive into the command line, let’s get the big picture of how Windows organizes files.

Key Concepts:

Example Structure:


C:\
 ├── Users\
 │   ├── John\
 │   │   ├── Documents\
 │   │   ├── Downloads\
 │   │   ├── Desktop\
 │   │   ├── Pictures\
 ├── Program Files\
 ├── Windows\
 ├── Temp\
            

Now, let’s navigate this like a pro using the command line!


Part 2: Navigating the File System with CMD & PowerShell

First, open the Command Prompt or PowerShell:

1. See Your Current Location

CMD:


echo %CD%
            

PowerShell:


pwd  # Returns the current directory
$PWD  # PowerShell object way, same result as `pwd`
            

When to use: In CMD, echo %CD% prints the current directory, and in PowerShell, pwd gives the current directory. The $PWD object is handy for scripts and interactive use.

2. List Files and Folders

CMD:


dir
            

PowerShell:


ls
            

When to use: Both commands list files in the current directory. Use dir /w in CMD for wide format or apply filters in PowerShell.

3. Move Around (Change Directory)

CMD:


cd C:\Users\John\Documents  # Move to Documents folder
cd ..                       # Move up one level
cd /d D:\Projects           # Switch to another drive
            

PowerShell:


cd C:\Users\John\Documents  # Move to Documents folder
cd ..                       # Move up one level
cd /d D:\Projects           # Switch to another drive (works like CMD)
Set-Location C:\Users\John  # Alternative PowerShell cmdlet
            

When to use: Use cd in CMD and PowerShell; for clearer scripts in PowerShell, consider Set-Location.

4. Create New Folders

CMD:


mkdir MyFolder
            

PowerShell:


mkdir MyFolder
New-Item MyFolder -ItemType Directory  # Alternative in PowerShell
            

When to use: Both approaches create directories; PowerShell’s New-Item provides extra control when needed.

5. Create Files

PowerShell:


New-Item myfile.txt -ItemType File
            

CMD:


echo "Hello World" > myfile.txt
            

When to use: PowerShell’s New-Item is ideal for scripted workflows, while CMD’s redirection is great for quick file creation.

6. Delete Files and Folders

CMD:


del myfile.txt
rmdir MyFolder
            

PowerShell:


Remove-Item myfile.txt
Remove-Item MyFolder -Recurse
            

When to use: CMD uses del and rmdir, while PowerShell’s Remove-Item can handle both files and folders with additional options.

7. Copy and Move Files

CMD:


copy myfile.txt C:\Backup\
            

PowerShell:


Move-Item myfile.txt C:\Backup\
Copy-Item myfile.txt C:\Backup\
            

When to use: Use CMD’s copy or PowerShell’s Copy-Item and Move-Item for file operations with added flexibility.


Part 3: Intermediate Command Line Power Moves

Once comfortable, let's move to more intermediate commands.

1. Open Programs

CMD:


start notepad.exe
            

PowerShell:


start notepad.exe
            

2. Check Your Disk Space

CMD:


wmic logicaldisk get size,freespace,caption
            

PowerShell:


Get-PSDrive
            

3. Find Files Containing Specific Text

CMD:


findstr "error" log.txt
            

PowerShell:


Select-String -Path log.txt -Pattern "error"
            

4. Copy Multiple Files at Once

CMD:


copy *.txt C:\Backup\
            

PowerShell:


Copy-Item *.txt -Destination C:\Backup\
            

5. Clean Up Temp Files

CMD:


del /q C:\Users\%USERNAME%\AppData\Local\Temp\*
            

PowerShell:


Remove-Item "$env:LOCALAPPDATA\Temp\*" -Force -Recurse
            

Final Challenge: Prove Your Command Line Captaincy

Try these:

Master these, and you're a Cap10 of the Command Line! 🏴‍☠️

Would you like a cheat sheet with all these commands for quick reference? 🚀

Key File Locations & Tools Available in the CLI

Now that you've learned how to navigate and manage files in the command line, it's important to understand where to find key system files and the tools you have access to in the CLI. Here's a breakdown of the most commonly used directories and utilities that will be helpful when you're working with files in Windows:

Key File Locations

Here are some important file locations in Windows that you will frequently interact with through the command line:

Note: Use the cd command with these paths (e.g., cd C:\Users\YourName\) to quickly navigate.

Essential Command Line Tools

Here are some useful tools and utilities available in the Windows command line:

Note: Most tools work in both CMD and PowerShell, though PowerShell cmdlets may offer extra features.

File Path Environment Variables

Environment variables let you access common directories without typing full paths. Examples include:

Using these variables (e.g., cd %TEMP%) can save you time.