Hi @AATester

Thanks for writing and I'm happy to help you today regarding running your power shell script.

In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ("").

Also try using the Grave Accent Character () PowerShell uses the grave accent () character as its escape character. Just add it before each space in the file name. (You’ll find this character above the Tab key and below the Esc key on your keyboard.)

WshShell.Run("powershell -file \"" + pathvar + "\*My `  Tests*\\PickupTest.ps1")  

Here is a helpful page with information about Windows PowerShell

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/powershell

------------

--If the reply is helpful, please Upvote and Accept as answer.--

Answer from Limitless Technology on learn.microsoft.com
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "powershell howtos" › "path with spaces in powershell"
Path With Spaces in PowerShell | Delft Stack
January 30, 2023 - You can use the & operator to invoke commands by enclosing the path with spaces in double quotes " ". ... Or, you can also use the . operator to run the command using the double quotes " " in the path.
🌐
Reddit
reddit.com › r/powershell › filepath with spaces in command line
r/PowerShell on Reddit: Filepath with spaces in command line
January 21, 2022 -

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.

🌐
Reddit
reddit.com › r/powershell › how to escape spaces in powershell while using double quotes ?
r/PowerShell on Reddit: How to escape spaces in powershell while using double quotes ?
August 12, 2023 -

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.

Top answer
1 of 4
18
How come you think that launching powershell that launches cmd that launched bat file is a good idea? Your issue has nothing to do with powershell - this is how cmd.exe, that you launch, looks at parameters - to be specific - /c key looks for a string between space right after /c and whatever next space, which happens to be part of your path. Proper solution - get rid of cmd /c, open install.cmd and adapt whatever is in there to powershell. Highly likely it just launches msi/exe file with a few parameters and lots of spaghetti crap to make it all work in bat (which is unneccessary in powershell) If you still insist on using this wonky stuff, you can utilize string concatenation, something like that: -ArgumentList $('/c "C:\Users\' + $env:username + '\Downloads\Installer\Silent Install.cmd"') Or, you know, just rename this install.cmd file to not have a space in it's name. Why it's even there?
2 of 4
14
The problem here is the lack of quotation marks around your -ArgumentList file path. As you're passing the path via an argument to cmd.exe, it must be properly formatted for cmd.exe (not PowerShell) to interpret. This means file paths with spaces must be wrapped with double quotation marks. # To fix your command, as-is: Start-Process -FilePath cmd.exe -ArgumentList "/c `"$Env:USERPROFILE\Downloads\Installer\_Silent Install.cmd`"" -NoNewWindow -Wait # Or... Start-Process -FilePath cmd.exe -ArgumentList ('/c "{0}"' -f "$Env:USERPROFILE\Downloads\Installer\_Silent Install.cmd") -NoNewWindow -Wait # Assuming the default cmdfile association is in place, explicitly launching cmd.exe is unnecessary. Start-Process -FilePath $Env:USERPROFILE\Downloads\Installer\_Silent Install.cmd -NoNewWindow -Wait # Rather than use Start-Process, run the batch file natively. # This is done synchronously in the same window, so PowerShell will invariably wait for completion. & "$Env:USERPROFILE\Downloads\Installer\_Silent Install.cmd"
🌐
Ultimate Systems Blog
blog.usro.net › 2025 › 06 › add-paths-in-powershell-scripts-spaces
Add Paths in PowerShell, Scripts & Spaces
July 15, 2026 - Always quote or escape spaces in paths. Validate after every change; broken PATH entries can stop commands from resolving. append to path powershell how to add directory to path powershell how to add file path in powershell script how to add ...
🌐
Directory Opus
resource.dopus.com › help & support
Run PowerShell script with spaces in script path & args? - Help & Support - Directory Opus Resource Centre
February 26, 2021 - Problem I created a toolbar button to run a script. However, I got the following error: The string is missing the terminator: '. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString The problem was that quotation marks and double quotes were processed by Directory Opus and erased before being passed to PowerShell.
Find elsewhere
🌐
How-To Geek
howtogeek.com › home › windows › how to escape spaces in file paths on the windows command line
How to Escape Spaces in File Paths on the Windows Command Line
September 18, 2023 - To escape spaces in a Windows file path, put double quotation marks around the section of the path with a space in it. For example: C:\"Test Folder"\text.txt. You can also put quotation marks around the entire path to ensure you don't miss any ...
🌐
Google Groups
groups.google.com › g › rundeck-discuss › c › 7vEdssOa1FU
Issues with spaces in powershell file path
June 9, 2022 - This works really well for us, with ONE exception (that I’ve found thus far). If we try to execute a powershell script with a space in it’s name, we get an error back. We have tried many different permutations of quoting the string.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Can't input a path with spaces in it - PowerShell Help - PowerShell Forums
April 25, 2019 - Hey everyone, I’m having trouble with a script i’m building to get informations from active directory. Here’s my code { #[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null Import-Module PowerShellAccessControl #$Chemin = [Microsoft.VisualBasic.Interaction]::InputBox("entrer un chemin") write-host " Z: (lettre de lecteur utilisée pour \\Aietes\Echange_RH) " -backgroundcolor black -foregroundcolor yellow write-host " X: (lettre de lecteur utilisée pour \\Aietes...
🌐
Microsoft
devblogs.microsoft.com › dev blogs › scripting blog [archived] › powertip: run a powershell script with space in the path
PowerTip: Run a PowerShell Script with Space in the Path - Scripting Blog [archived]
August 7, 2012 - Summary: Learn how to run a script with a space in the path. Question: How do I run a script with a space in the path? Answer 1: PS > c:my` foldermyscript.ps1 Answer 2: PS> &(“c:my foldermyscript.ps1”) Question: What is the easiest way to create an array?
🌐
Paul Cunningham
paulcunningham.me › home › using invoke-expression in powershell with spaces in paths
Using Invoke-Expression in PowerShell With Spaces in Paths
November 4, 2025 - The other option is to escape the spaces in the path, for example: PS C:\Scripts> $document = "C:\Scripts\Test Document.html" PS C:\Scripts> $document = $document -replace ' ','` ' PS C:\Scripts> Invoke-Expression $document · The first option is probably better, as it doesn’t require you to modify the existing variable or set a new variable for the path with escaped spaces.
🌐
Advanced Installer Community
advancedinstaller.com › board index › advanced installer software › common problems
PowerShell - paths with spaces - Advanced Installer Community
September 21, 2016 - --> Found PowerShell path: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe --> PowerShell Script Execution Result Code: 574 --> PowerShell Script Execution log: Start-Process -FilePath "run.bat" -WorkingDirectory C:\Program Files\Test\service\ C:\Users\IEUser\AppData\Local\Temp\pss2AB4.p...
Top answer
1 of 10
1

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
2 of 10
5

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!

🌐
Reddit
reddit.com › r/powershell › how am i supposed to deal with spaces in file paths inside a pipeline
r/PowerShell on Reddit: How am I supposed to deal with spaces in file paths inside a pipeline
January 24, 2023 -

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"

Top answer
1 of 5
60

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
2 of 5
44

Try this:

& "C:\Users\test\Documents\test\line space\PS Script\test"
🌐
Daniel's Programming Blog
blog.danskingdom.com › fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces
Fix Problem Where Windows PowerShell Cannot Run Script Whose Path Contains Spaces - Daniel Schroeder’s Programming Blog
May 28, 2013 - When %~dp0 resolves to a path that has NO spaces in it, the script runs fine. IE… · call powershell.exe -ExecutionPolicy Bypass -Command & “c:\pwrscripts\Removeit\uninstall-it.ps1” · However, when it resolves to a path with a space in it I get an error message and in effect the problem with the space in the path is the cause.
🌐
Reddit
reddit.com › r/powershell › test-path doesn't work with spaces inside a variable
r/PowerShell on Reddit: Test-Path doesn't work with spaces inside a variable
June 9, 2022 -

I've got a text file containing some exe's which looks like this:

C:\WINDOWS\system32\cmd.exe

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

C:\WINDOWS\system32\notepad.exe

"C:\Program Files (x86)\Windows Photo Viewer\ImagingDevices.exe"

However when the following commands are used:

$Programs = Get-Content -Path 'C:\temp\AdministrativeAccess\ProgramList.txt'

foreach ($Program in $Programs) { Test-Path $Program }

I get the result: True True True False

All the files exist so all should 'True'. What am I doing wrong?