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.
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
This worked for me:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned C:\Program Files\My` Scripts\CenterCursor
Note that you will need a ` after each word (before the space) and you don't need the outer ".
Source
In a more general case, you can use the following syntax to run any PowerShell script with space in the path:
&("C:\any path with spaces in it\")
In your case, that would be:
&("C:\Program Files\My Scripts\CenterCursor")
Edit: this only works if you are using windows powershell
Source
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.
i am trying to execute a command in powershell wherein it contains spaces in the file path. After looking around, i found out that i need to escape the spaces using ` symbol and use single quotes to handle spaces properly. The issue is that my command contains variables as well so i need to use double quotes to get them to expand while getting executed. is there any way out ? Including example below wherein it only considers the path until the space character
Start-Process -FilePath "cmd.exe" -ArgumentList "/c C:\Users\$env:username\Downloads\Installer\_Silent` Install.cmd" -NoNewWindow -Wait
'C:\Users\Admin\Downloads\Installer\_Silent' is not recognized as an internal or external command, operable program or batch file.
this seems to work just fine for me.
$foldername = "d:\ test folder"
$folder1quoted = "$foldername\2021"
$folder2quoted = "$foldername\2022"
if (Test-Path $folder1quoted) {
icacls $folder1quoted
}
if (Test-Path $folder2quoted) {
icacls $folder2quoted
}
d:\ test folder\2021 BUILTIN\Administrators:(I)(F)
BUILTIN\Administrators:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(I)(F)
NT AUTHORITY\SYSTEM:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\Authenticated Users:(I)(M)
NT AUTHORITY\Authenticated Users:(I)(OI)(CI)(IO)(M)
BUILTIN\Users:(I)(RX)
BUILTIN\Users:(I)(OI)(CI)(IO)(GR,GE)
Successfully processed 1 files; Failed processing 0 files
d:\ test folder\2022 BUILTIN\Administrators:(I)(F)
BUILTIN\Administrators:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(I)(F)
NT AUTHORITY\SYSTEM:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\Authenticated Users:(I)(M)
NT AUTHORITY\Authenticated Users:(I)(OI)(CI)(IO)(M)
BUILTIN\Users:(I)(RX)
BUILTIN\Users:(I)(OI)(CI)(IO)(GR,GE)
Successfully processed 1 files; Failed processing 0 files
Hi all. I’m not a programmer, but I do a fair bit of scripting. Most of it is ugly and inefficient but it gets the job done 
I have always struggled to deal with spaces in paths, especially within variables.
Here’s today’s problem. A script to assign permissions in subfolders of a client data folder. (long story why we need it…) Also yes, I could use set-acl instead of icacls and will soon, or do it all in a .cmd file and use if exist etc. But I want to PS. 
Also this is just a handy example, I have other scripts that suffer as well.
Example user needs to run a script to set permissions on F:\a\abc corp (11010)\2020 and F:\a\abc corp (11010)\2021
If I force double quotes into the variable, the folder path gets passed to the command and works fine, but the Powershell test-path fails. If I don’t force the double quotes the test-path works, but the path gets passed to the command without quotes and fails.
With quotes:
param(
$foldername
)
$folder1quoted = '"'+$foldername +'\2021"'
$folder2quoted = '"'+$foldername +'\2022"'
if (Test-Path $folder1) {
icacls $folder1quoted /grant Group:'(CI)(OI)M'
icacls $folder1quoted /grant 'Other Group:(CI)(OI)M'
}
if (Test-Path $folder2) {
icacls $folder2quoted /grant Group:'(CI)(OI)M'
icacls $folder2quoted /grant 'Other Group:(CI)(OI)M'
}
There, icacls would work, before I added the Test-path but when I add the test-path so the user doesn’t see all the errors for non-existent folders, test-path always fails.
Without Quotes:
param(
$foldername
)
$folder1 = $foldername +'\2020'
$folder2 = $foldername +'\2021'
if (Test-Path $folder1) {
icacls $folder1quoted /grant Group:'(CI)(OI)M'
icacls $folder1quoted /grant 'Other Group:(CI)(OI)M'
}
if (Test-Path $folder2) {
icacls $folder2quoted /grant Group:'(CI)(OI)M'
icacls $folder2quoted /grant 'Other Group:(CI)(OI)M'
}
icacls obviously fails.
So right now I’ve combined the 2. 1 set of vars with quoted to pass to icacls, and 1 set without, for the Test-Path. But I’d like to learn how to do it better.
I often use this method of adding quotes for software installs where there’s a base-path \server\installs and a product path \server\installs\program1 it’s messy and tedious to type.
I use variables because there are always new versions and 1 script goes a long way when you just change a few vars, and save it as a new script.
Thanks in advance!
Im trying to copy all the excel files from a +100gb directory with this command:
Get-ChildItem -Recurse -Filter *.xlsx | select FullName | Copy-Item -Destination C:\Users\exampleuser\Desktop\xlsx_files\
Unfortunately all the files from the source directory have multiple spaces in the path, and I know no way of adding quotes around the paths while theyre inside the pipeline. How would I do this?
Edit: One of the paths inside the pipeline might look like this: "K:\DepartmentC\A lot of files in here\Team 2\description.xlsx"
The -File parameter
If you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ("). Single quotes (') are only recognized by PowerShell. But as powershell.exe is invoked (and hence the file parameter processed) by the command line, you have to use ".
powershell.exe -File "C:\Users\test\Documents\Test Space\test.ps1" -ExecutionPolicy Bypass
The -Command parameter
If you use the -Command parameter, instead of -File, the -Command content is processed by PowerShell. Hence you can - and in this case have to - use ' inside ".
powershell.exe -Command "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass
The double quotes are processed by the command line, and & 'C:\Users\test\Documents\Test Space\test.ps1' is a command that is actually processed by PowerShell.
Solution 1 is obviously simpler.
Note that -Command is also the default parameter that is used, if you do not specify any.
powershell.exe "& 'C:\Users\test\Documents\Test Space\test.ps1'" -ExecutionPolicy Bypass
This would work, too.
The -EncodedCommand parameter
You can encode your command as Base64. This solves many "quoting" issues and is sometimes (but not in your case though) the only possible way.
First you have to create the encoded command
$Command = "& 'C:\Users\test\Documents\Test Space\test.ps1'"
[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Command))
And then you can use the the -EncodedCommand parameter like this
powershell.exe -EncodedCommand JgAgACcAQwA6AFwAVQBzAGUAcgBzAFwAdABlAHMAdABcAEQAbwBjAHUAbQBlAG4AdABzAFwAVABlAHMAdAAgAFMAcABhAGMAZQBcAHQAZQBzAHQALgBwAHMAMQAnAA== -ExecutionPolicy Bypass
Try this:
& "C:\Users\test\Documents\test\line space\PS Script\test"