How to Copy a File Path That Works in the Terminal
Copying a file path from Windows Explorer and pasting it into a terminal seems like it should just work. It usually does not. Explorer's "Copy as path" returns a quoted Windows path: "C:\Users\name\project\file.txt". PowerShell accepts that. cmd accepts that. Bash does not. WSL does not. Python's open() will accept it but with caveats. A developer who pastes the path into the wrong target gets a syntax error, a file-not-found, or — worse — a successful execution against the wrong file.
This guide is about copying file paths that work in each terminal target. It is written for developers using Windows 10 and 11 who move between Explorer, PowerShell, cmd, bash, WSL, and Python. For neighbouring topics, see API keys on the clipboard are an incident, diffing two copied code snippets, and clipboard habits that keep secrets off the stack.
What Explorer's "Copy as path" returns
In Windows 11, right-clicking a file in Explorer shows "Copy as path" directly in the context menu. In Windows 10, hold Shift while right-clicking to see the option. The result is a string like:
"C:\Users\name\project\file.txt"
The double quotes are part of the copied string. They are necessary because Windows paths can contain spaces, and unquoted spaced paths break in most shells. The backslashes are the Windows separator.
This format works in PowerShell and cmd, with minor caveats. It does not work in bash, WSL, or most cross-platform tools without translation.
Target: PowerShell
PowerShell accepts the quoted Windows path directly. Pasting "C:\Users\name\project\file.txt" into a Test-Path, Get-Content, or Copy-Item command works as expected.
Test-Path "C:\Users\name\project\file.txt"
Get-Content "C:\Users\name\project\file.txt"
Copy-Item "C:\Users\name\project\file.txt" "C:\Users\name\project\file.bak"
Caveats:
- Backslashes in strings. PowerShell treats backslashes as literal characters in single-quoted strings and as escape characters in double-quoted strings. The path above is in double quotes, so a path containing
\t,\n, or\rwould be interpreted as a tab, newline, or carriage return. For paths containing these sequences, use single quotes:'C:\Users\name\test\file.txt'. - PowerShell variables. Paths containing
$(rare but possible) will be interpreted as variable references in double-quoted strings. Use single quotes or escape the$.
The safest pattern: paste the path, then if it contains t, n, r, or $ after a backslash, change the surrounding quotes from double to single.
Target: cmd
cmd accepts the quoted Windows path directly. Pasting "C:\Users\name\project\file.txt" into a dir, type, or copy command works.
dir "C:\Users\name\project\file.txt"
type "C:\Users\name\project\file.txt"
copy "C:\Users\name\project\file.txt" "C:\Users\name\project\file.bak"
cmd does not have PowerShell's escape-sequence issue; backslashes are always literal. The quotes are still required for paths with spaces.
Target: bash (Git Bash, MSYS2)
Bash on Windows (Git Bash, MSYS2) uses forward slashes and a /c/ prefix for Windows drives. The Explorer-copied path does not work as-is. Translation required:
| Explorer form | Bash form |
|---|---|
"C:\Users\name\file.txt" | /c/Users/name/file.txt |
"D:\Projects\app" | /d/Projects/app |
Translation steps:
- Strip the surrounding quotes.
- Replace the colon after the drive letter with nothing.
- Convert backslashes to forward slashes.
- Lowercase the drive letter (optional but conventional).
For interactive use, a shell function helps:
win2bash() {
echo "$1" | sed 's|"||g; s|\\|/|g; s|^\([A-Za-z]\):|/\L\1|'
}
Then win2bash "C:\Users\name\file.txt" returns /c/Users/name/file.txt.
Target: WSL
WSL2 uses forward slashes and a /mnt/c/ prefix for Windows drives. The translation is similar to bash but with a different prefix:
| Explorer form | WSL form |
|---|---|
"C:\Users\name\file.txt" | /mnt/c/Users/name/file.txt |
"D:\Projects\app" | /mnt/d/Projects/app |
The wslpath tool handles this automatically:
wslpath -u "C:\Users\name\file.txt"
# Returns: /mnt/c/Users/name/file.txt
For interactive use, wslpath is the right tool. For scripts, define an alias or function:
win2wsl() {
wslpath -u "$1" 2>/dev/null
}
See WSL and Windows clipboard: what crosses the boundary for the full WSL clipboard workflow.
Target: Python
Python's open() accepts Windows paths in several forms:
- Raw string with backslashes:
r"C:\Users\name\file.txt"— therprefix prevents\U,\N,\tetc. from being interpreted as escape sequences. - Forward slashes:
"C:/Users/name/file.txt"— forward slashes work on Windows in Python'sopen(). - Double backslashes:
"C:\\Users\\name\\file.txt"— each backslash is escaped. - Quoted path with single backslashes:
"C:\Users\name\file.txt"— works for paths without\U,\N,\t,\n,\rsequences; fails for paths that contain them.
The safest pattern: paste the path, then prefix with r to make it a raw string. The raw string preserves backslashes literally, so r"C:\Users\name\test\file.txt" works where "C:\Users\name\test\file.txt" would fail (because \t would be interpreted as a tab).
For cross-platform Python, prefer pathlib.Path:
from pathlib import Path
p = Path(r"C:\Users\name\file.txt")
pathlib.Path normalises the path to the platform's convention and handles path manipulation safely.
Target: Node.js
Node.js accepts Windows paths in JavaScript strings. The same escape-sequence caveat applies: backslashes in regular strings are escape characters. Use one of:
- Raw template literal: `
C:\\Users\\name\\file.txt— double backslashes work because each\\` is an escaped backslash. - Forward slashes:
"C:/Users/name/file.txt"— Node'sfsmodule accepts forward slashes on Windows. path.join:path.join("C:", "Users", "name", "file.txt")— builds the path correctly per platform.
For interactive use, paste the path, then double every backslash. The doubling is mechanical but error-prone; path.join is safer for code.
Common failure modes
A few specific failures are worth knowing:
- The unquoted spaced path.
C:\Program Files\app\app.exepasted into a terminal will be parsed asC:\Programfollowed by argumentsFiles\app\app.exe. Always quote spaced paths. - The
\tin a Python string."C:\Users\name\test\file.txt"becomes"C:\Users\name<TAB>est\file.txt"because\tis a tab. User"..."or forward slashes. - The backslash in JSON. A pasted Windows path in a JSON string needs every backslash doubled:
"C:\\Users\\name\\file.txt". JSON parsers reject unescaped backslashes. - The WSL path in a Windows tool. Pasting
/mnt/c/Users/name/file.txtinto a Windows tool (Explorer, Notepad) fails because the Windows tool does not understand the/mnt/prefix. - The Windows path in WSL. Pasting
C:\Users\name\file.txtinto a WSL bash command fails because bash does not understand the drive-letter convention.
For each of these, the fix is translation — see the patterns above.
What to do for paths with special characters
Some paths have characters that break even the correct shell. Knowing the patterns in advance saves debugging time.
- Ampersand in the path (
C:\Users\name\Documents\R&D\file.txt). cmd and PowerShell accept this with quoting, but bash interprets&as a background operator. In bash, escape the&as\&or use single quotes. - Parentheses (
C:\Users\name\Documents\Dev(x64)\file.txt). Same as ampersand — Windows shells handle it with quotes; bash needs escaping or single quotes. - Unicode characters (
C:\Users\名前\file.txt). All shells handle Unicode if the terminal is set to UTF-8, but the file system on Windows uses UTF-16 internally and the translation layer can produce surprising results. If a path with Unicode characters is failing, paste it into a file and read the file rather than pasting directly. - Long paths (over 260 characters). Windows has a 260-character path limit by default; paths longer than that fail in most tools regardless of quoting. Enable long path support in Windows (Group Policy or registry) or use the
\\?\prefix to bypass the limit. - Network paths (
\\server\share\file.txt). UNC paths work in Windows shells; in WSL, they translate to/mnt/smb/server/share/file.txtif mounted, or they need to be accessed viasmbclient.
For each, the rule is the same: know what the target shell expects, translate accordingly, and use raw strings in Python and double-quoted backslashes in Node.js to avoid escape-sequence issues.
Why this matters for automation
The translation patterns above are manual for interactive use. For automation (scripts, CI pipelines, build systems), the translation needs to be built into the code. Most path-handling libraries (pathlib.Path in Python, path.join in Node.js, pathlib in Rust, os.path in Go) handle the translation automatically when used correctly. The mistake is reaching for string concatenation instead of the library.
A script that builds a Windows path by string concatenation ('C:\\Users\\' + user + '\\' + filename) is fragile: it breaks if the user has a space in their name, if the filename has a backslash, if the path is on a network share, or if the script is run on a non-Windows platform. The same script using pathlib.Path('C:/Users', user, filename) handles all of those cases correctly.
The clipboard translation patterns above are the interactive version of what path libraries do in code. Knowing both makes you faster at the keyboard and better at writing robust scripts.
A practical workflow
For developers who paste paths into terminals all day, the workflow that works:
- Copy as path from Explorer (
Shift+right-click → Copy as path, or right-click → Copy as path in Windows 11). - Paste into the target. If the target accepts Windows paths (PowerShell, cmd, Python with
r"..."), it works. - If the target does not accept Windows paths (bash, WSL), translate with
wslpathor thewin2wslfunction before pasting. - For paths in code, use the target language's path library (
pathlib.Pathin Python,path.joinin Node.js) rather than pasting raw strings.
Related reading
- Diffing Two Copied Code Snippets
- Regex Testers, JWT Debuggers, and Clipboard Hygiene
- Best Clipboard Habits for Developers in 2026
- Clipboard Tools on Multi-Monitor Windows Setups
Sources
- Microsoft Support — Copy as path — official Windows guidance for the "Copy as path" feature in Explorer, including the keyboard shortcut
- Microsoft Learn — PowerShell about quoting Rules — official documentation for PowerShell string quoting, including the single-quote vs double-quote escape behaviour
- Microsoft Learn — WSL wslpath command — official documentation for
wslpath, the WSL tool that translates paths between Windows and WSL - Python documentation — pathlib module — official documentation for
pathlib.Path, the recommended cross-platform path handling class in Python - Node.js documentation — path module — official documentation for Node's
pathmodule, includingpath.joinfor safe path construction
Deepender Yadav is a B.Tech Computer Science Engineering student and software developer interested in building practical software and open-source projects.
GitHub · LinkedInCopy. Stack. Drop.
Transform your clipboard into an interactive edge shelf. Stack, pin, and drag assets into any app with zero friction.
Download for Windows Get from Microsoft Store
How to Install Guide · First 10 Minutes Guide · Drag & Drop Guide · Edge-Drop vs Win+V · Support
Free · Lightweight · Privacy First