123

I'm having an issue with powershell when invoking an exe at a path containing spaces.

PS C:\Windows Services> invoke-expression "C:\Windows Services\MyService.exe"

The term 'C:\Windows' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

It seems to be splitting on the space between 'Windows' and 'Services'. Any idea how to get around this problem?

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
jaffa
  • 26,770
  • 50
  • 178
  • 289
  • 1
    Use [`&`, the call operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#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](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/). – mklement0 Aug 10 '19 at 16:36

16 Answers16

182

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.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Adil Hindistan
  • 6,351
  • 4
  • 25
  • 28
  • 1
    For me, I am passing the path as an -ArgumentList into Start-Process. Start-Process -FilePath "notepad++.exe" -ArgumentList "C:\Long Path\That\contains\many spaces\toThisFile.txt" – James Aug 08 '22 at 14:02
32

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
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • Problem with this solution is that the path is passed in as an argument and I cannot control how it is escaped. – jaffa Aug 30 '13 at 16:15
  • 1
    You can just perform `-replace` on the variable containing the path and replace ' ' with '` ' – Anthony Neace Aug 30 '13 at 16:16
  • 2
    While this answers the question as asked, it's worth pointing out that `Invoke-Expression` is not only the wrong tool to use for the OP's use case, [it should _generally_ be avoided](https://blogs.msdn.microsoft.com/powershell/2011/06/03/invoke-expression-considered-harmful/). – mklement0 Aug 10 '19 at 16:37
14

Not sure if someone still needs it... I needed to invoke msbuild in powershell and following worked fine:

$MSBuild = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"

& $MSBuild $PathToSolution /p:OutDir=$OutDirVar /t:Rebuild /p:Configuration=Release
Jacek Plesnar
  • 479
  • 3
  • 6
12
"&'C:\Windows Services\MyService.exe'" | Invoke-Expression

via https://www.vistax64.com/powershell/52905-invoke-expression-exe-has-spaces-its-path.html

Alan
  • 169
  • 1
  • 3
11

For any file path with space, simply put them in double quotations will work in Windows Powershell. For example, if you want to go to Program Files directory, instead of use

PS C:\> cd Program Files

which will induce error, simply use the following will solve the problem:

PS C:\> cd "Program Files"
CathyQian
  • 1,081
  • 15
  • 30
8

This worked for me:

$scanresults = Invoke-Expression "& 'C:\Program Files (x86)\Nmap\nmap.exe' -vv -sn 192.168.1.1-150 --open"
Samuel Philipp
  • 10,631
  • 12
  • 36
  • 56
J. Fischlein
  • 91
  • 1
  • 1
  • Thank you very much! This worked to launch a powershell file named `wslLauncher.ps1` that was located in a path that contained a space, from Eclipse in Java. The exact line of code was: ```String command = "powershell.exe & '"+launcherPath+launcherName+"' ";``` – a.t. Jun 05 '19 at 15:10
7

Using Powershell on Windows10 in 2018, what worked for me was simply to replace double quotes " by simple quotes '. Adding the backtick before the space, as suggested in an answer, broke the path.

seinecle
  • 10,118
  • 14
  • 61
  • 120
  • Works for me. If I use double quote, I get the error `ParameterArgumentValidationError`. – jdhao Feb 01 '19 at 10:08
7

What worked for me (I needed the path to create a MySQL dump), was to place the directory in between 6 double quotes like so:

$path = """C:\Path\To\File"""
double-beep
  • 5,031
  • 17
  • 33
  • 41
MvH
  • 79
  • 1
  • 1
6

Can use the . dot operator.

. "C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd"

or the Start-Process command

Start-Process -PSPath "C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd"

or using ProcessStartInfo and Process

$ProcessInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
$ProcessInfo.FileName = 'C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe'
if($Admin){ $ProcessInfo.Verb = 'runas' }
$ProcessInfo.UseShellExecute = $false

$CommandParameters = '-noexit -noprofile -command Set-Location -LiteralPath c:\; $host.ui.RawUI.WindowTitle = ''[{0}] PS''; Set-PSReadlineOption -HistorySaveStyle SaveNothing;' -f $Cred.UserName
$ProcessInfo.Arguments = $CommandParameters
$ProcessInfo.Domain = ($Cred.UserName -split '\\')[0]
$ProcessInfo.UserName = ($Cred.UserName -split '\\')[1]
$ProcessInfo.Password = $Cred.Password

$ProcessObject = New-Object -TypeName System.Diagnostics.Process
$ProcessObject.StartInfo = $ProcessInfo
$ProcessObject.Start() | Out-Null
jkdba
  • 2,378
  • 3
  • 23
  • 33
3

There's a hack I've used since the Invoke-Expression works fine for me.

You could set the current location to the path with spaces, invoke the expression, get back to your previous location and continue:

$currLocation = Get-Location
Set-Location = "C:\Windows Services\"
Invoke-Expression ".\MyService.exe"
Set-Location $currLocation

This will only work if the exe doesn't have any spaces in its name.

Hope this helps

c-chavez
  • 7,237
  • 5
  • 35
  • 49
2

Please use this simple one liner:

Invoke-Expression "C:\'Program Files (x86)\Microsoft Office\root\Office16'\EXCEL.EXE"
albertoiNET
  • 1,280
  • 25
  • 34
pramod
  • 21
  • 1
1

Try this, simple and without much change:

invoke-expression "'C:\Windows Services\MyService.exe'"

using single quotations at the beginning and end of the path.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
djsp
  • 19
  • 2
  • This worked for me when I needed to invoke a powershell script from bash and double quotes or triple double quotes weren't cutting it. – Tilwaen Mar 25 '22 at 08:17
0

Due to several PowerShell quoting rules that are used for passing arguments to native commands which also have specific experimental Features in PowerShell, quoting might get very confusing. Therefore the solution in some cases might be simply to don't go down the rabbit hole:

Avoid quoting file paths with spaces by using the 8.3 filenames

$FSO = New-Object -ComObject Scripting.FileSystemObject
$FSO.getfile('C:\Windows Services\MyService.exe').ShortPath
C:\WINDOW~1\MYSERV~1.EXE
iRon
  • 20,463
  • 10
  • 53
  • 79
-1

Just put ${yourpathtofile/folder}

PowerShell does not count spaces; to tell PowerShell to consider the whole path including spaces, add your path in between ${ & }.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
GSKKC
  • 45
  • 6
-1

Simply put the path in double quotes in front of cd, Like this:

cd "C:\Users\MyComputer\Documents\Visual Studio 2019\Projects"

Shoaib Khalil
  • 1,874
  • 17
  • 10
-1
  1. enter the root C drive by entering command

C:

  1. type cd and then press Tab key, it will toggle through all available locations and press enter when you have reached the desired one

cd {press tab}

techloris_109
  • 547
  • 5
  • 13