Type cd c:\ . Now press the tab key (this is auto-complete, which will save you a lot of typing) repeatedly until it shows you a directory with spaces in the name. Notice how it delimits the path with apostrophes '. Use the same character when you're typing.
Edit: Make sure to use an apostrophe, not a backtick.
Answer from Andrew Morton on Stack ExchangeType cd c:\ . Now press the tab key (this is auto-complete, which will save you a lot of typing) repeatedly until it shows you a directory with spaces in the name. Notice how it delimits the path with apostrophes '. Use the same character when you're typing.
Edit: Make sure to use an apostrophe, not a backtick.
You need to surround the path in double quotes if there is a space in the path!
cd "C:\Program Files"
SFTP cd (or lcd) powershell change to dir with space in path
how do i use cd to go to a directory with a space in the name?
powershell - What do I put instead of a space when changing directory? - Stack Overflow
Add support for cd (Set-Location) to path with spaces without requiring quotes
Would this do what you want?:
& "C:\Windows Services\MyService.exe"
Use &, the call operator, to invoke commands whose names or paths are stored in quoted strings and/or are referenced via variables, as in the accepted answer. Invoke-Expression is not only the wrong tool to use in this particular case, it should generally be avoided.
You can escape the space by using single quotations and a backtick before the space:
$path = 'C:\Windows Services\MyService.exe'
$path -replace ' ', '` '
invoke-expression $path
Just starting to learn how to navigate the file system in the terminal, and I am having trouble with the fact that the majority of my files have spaces in their names (at least i think thats the problem)
So I have a foulder named "key backup" that I want to navigate to, I would type
cd /home/Documents/key backup
but then i get an error
bash: cd: too many arguments
I presume this is because there is a space, because i when i use the GUI to rename the foulder to keybackup and try
cd /home/Documents/keybackup
it works fine. Is there a way i can go to directories with spaces in their names?
So this is probably really easy, but I am a Luddite.
What I’m doing: Using a .vbs to call a power shell command line as an admin, then add a windows service using:
-command New-Service -Name “”ServiceNameHere”” -BinaryPathName “”C:\Program Files(x86)…….””
I’m running into issues with the filepath - both the spaces and the parenthesis for (x86) are causing “Invalid argument: (:) [New-Service], ParameterBindingException” and “FullyQualifiedErrorId : MissingArgument”
I don’t have these issues when I type the command into the PS console manually, only when it’s brought in from my vbs script.
$ cd "$DOCS"
You need to quote "$DOCS" to prevent spaces from being parsed as word separators. More often than not, variable references should be quoted.
Note that $HOME would have the same problem. The issue is coming from when the shell evaluates variable references; it's nothing to do with what variables you use or how you assign to them. It's the expansion that needs to be quoted.
$ echo $HOME
/home/my dir
This is deceptive. echo is actually echoing the two strings /home/my and dir. If you use cd or ls you'll see how it's actually working.
$ ls $HOME
ls: cannot access /home/my: No such file or directory
ls: cannot access dir: No such file or directory
$ cd $HOME
bash: cd: /home/my: No such file or directory
$ cd "$HOME"
<success!>
Can I ask why it works when I manually type it in but not in a variable?
Great question! Let's examine the commands you typed:
$ DOCS="\"/cygdrive/c/Users/my dir/Documents\""
$ echo $DOCS
"/cygdrive/c/Users/my dir/Documents"
$ cd $DOCS
-bash: cd: "/cygdrive/c/Users/my: No such file or directory
The reason this doesn't work is because Bash doesn't parse quotes inside variable expansions. It does perform word splitting, so whitespace in unquoted variable expansions is taken as word separators. It doesn't parse quotes in any way, meaning you can't put double quotes inside a variable to override word splitting.
$ cd $DOCS
Because of this, cd is passed two parameters. As far as cd knows it looks like you wrote:
$ cd '"/cygdrive/c/Users/my' 'dir/Documents"'
Two parameters, with double quotes intact.
SOLUTION:
cd "Documents and Photos"
problem solved.
The reason I'm submitting this answer is you'll find that StackOverflow is being used by every day users (not just web devs, programmers or power users) and this was the number one result for a simple Windows user question on Google.
People are becoming more tech-savvy, but aren't necessarily familiar with command line in the cases above.
Greeting's, (Please pardon me if this is too naive question, programming / forte isn't my forte.)
I have been handed a PowerShell script that executes to install an executable which is placed in the same directory. This is packaged into SCCM (System Center Configuration Manager) for deployment. However, the deployment fails if during package deployment the temporary director where the package is pushed has a space anywhere in the file path.
I've pasted the code here - https://pastebin.com/6KBqxgNG
For example:
-
C:\temp\deployment -- This will pass and the software will install.
-
C:\temp\deployment 1\** -- This will **fail due to space between deployment and 1
-
C:\temporary directory\deployment\ --** This will fai**l due to space between temporary and directory.
*Removed code due to formatting issues. Please refer to pastebin link for the code.*
I suspect following two lines to be the cause of the problem:
$tempFile = [System.IO.Path]::GetTempFileName() + ".cmd"
\$installLine = "$scriptPath\softwarename.exe /s /v"/qn\\```
I suspect that since execution will require exact path and a "space" in the directory path will lead to "path not found" (interestingly on my Windows 10 machine change directory [cd] command works without putting filepath with spaces in quotes - "" -- This leads me to believe I'm completely wrong, however at this point I have no other thing to look at).
Can anyone please assist on adding parameters to ensure that patch generated for .cmd file to be executed does not have a space?
I have tried to patch this by adding a batch file that copies the package to a static directory before execution. However, this is failing to deploy via SCCM.
I'd greatly appreciate
You appear to have noted the issue with the spaces in the file path, but other characters in this path are also causing problems, including the square brackets. The simplest solution is to tell PowerShell that the path is literally a path, literally.
So, use the 'LiteralPath' parameter:
cd -LiteralPath 'W:\_DITRH\__Featured Video Placeholder\__TISTL\10 [20151228] TISTL Sofia Smallstorm 10'
You need to use the single quote character ' as the path contains spaces and escape the brackets using the backtick `
cd 'W:\_DITRH\__Featured Video Placeholder\__TISTL\10 `[20151228`] TISTL Sofia Smallstorm 10'