Well, this is a cmd.exe problem, but there are some ways to solve it.

  1. Use single quotes

    powershell test.ps1 -x 'hello world' -y 'my friend'
    
  2. Use the -file argument

    powershell -file test.ps1 -x "hello world" -y "my friend"
    
  3. Create a .bat wrapper with the following content

    @rem test.bat
    @powershell -file test.ps1 %1 %2 %3 %4
    

    And then call it:

    test.bat -x "hello world" -y "my friend"
    
Answer from Nikolay K on Stack Overflow
🌐
SS64
ss64.com › ps › syntax-esc.html
PowerShell Escape characters
Delimiters separate one parameter from the next - they split the command line up into words. The standard delimiter for PowerShell is the space character, in addition the split operator and import-csv both accept options for splitting strings with a choice of delimiter.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_special_characters
about_Special_Characters - PowerShell | Microsoft Learn
January 16, 2025 - ... The null (`0) character appears as an empty space in PowerShell output. This functionality allows you to use PowerShell to read and process text files that use null characters, such as string termination or record termination indicators.
🌐
Reddit
reddit.com › r/powershell › spaces between characters in powershell ise console
r/PowerShell on Reddit: Spaces between characters in PowerShell ISE console
September 22, 2021 -

Hello,

When I execute ls command in PowerShell ISE console everything goes fine. But when I execute wsl I got too many spaces between lines and characters. Why I'm getting this? How to solve this problem?

PS C:\Windows\system32> wsl -l

W i n d o w s S u b s y s t e m f o r L i n u x D i s t r i b u t i o n s :

U b u n t u - 2 0 . 0 4 ( D e f a u l t )

d o c k e r - d e s k t o p

d o c k e r - d e s k t o p - d a t a

D e b i a n

🌐
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 - PowerShell uses the grave accent ( ` ) character as its escape character. Just add it before each space in the file name.
🌐
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"
🌐
Rakhesh
rakhesh.com › powershell › using-powershell-to-insert-a-space-between-characters-or-using-powershell-to-show-certificate-fingerprints-in-a-friendly-format
Using PowerShell to insert a space between characters (or: Using PowerShell to show certificate fingerprints in a friendly format) – rakhesh.com
March 9, 2015 - The -split operator can take a regular expression as the delimiter. Whenever the expression matches, the matched characters are considered to identify the end of the sub-string, and so the part before it is returned.
🌐
GitHub
github.com › cmderdev › cmder › issues › 2038
Quoted PowerShell script parameters with space characters trimmed after first space · Issue #2038 · cmderdev/cmder
February 11, 2019 - If I pass a command-line parameter value containing a space character enclosed inside of double quotes (e.g. -Config "A B C") to a PowerShell script, the value gets trimmed after the first space character. If I pass the same value enclosed in single quotes, it works as expected.
Author: cmderdev
Find elsewhere
🌐
EDUCBA
educba.com › home › data science › data science tutorials › powershell tutorial › powershell escape character
PowerShell Escape Character | List of Escape Sequence in PowerShell
October 11, 2024 - This escape sequence is used to move the position of cursor one position back but characters are not deleted. ... Write-Host "Example of back space escape sequene" $input= "Vignesh Krishnakumar" Write-Host "Actual word is "$input Write-Host "after using back space" Write-Host "vignesh`b`b`bkrishnakumar"
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Techotopia
techotopia.com › index.php › Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
Windows PowerShell 1.0 String Quoting and Escape Sequences - Techotopia
To understand the escape character, consider the first example in this chapter, whereby PowerShell interpreted the second word after the -inputobject paramater as an additional argument: PS C:\Users\Administrator> write-output -inputobject hello everyone Write-Output : A parameter cannot be found that matches parameter name 'everyone'. At line:1 char:13 + write-output <<<< -inputobject hello everyone · The reason this occurred was because the space character was interpreted not as a space, but as the delimiter between command line tokens.
🌐
Level1Techs
forum.level1techs.com › dev zone › code
Powershell script and blank spaces... seeking elegance and maybe comprehension - Code - Level1Techs Forums
January 30, 2022 - I don’t do much scripting and wanted to know what I could do to make the script I came up with more elegant in powershell. TBH, I’m a little baffled why powershell is keeping a space unless I select a non-spaced space. Explanation: I had a flat directory with file names which included where ...
🌐
PowerShell Test-Path
powershellfaqs.com › check-if-a-string-contains-a-space-in-powershell
How to Check if a String Contains a Space in PowerShell?
September 3, 2024 - To check if a string contains a space in PowerShell, you can use the -match operator with the regular expression pattern \s, which matches any whitespace character.
🌐
GitHub
github.com › microsoft › vscode › issues › 33584
Terminal doesn't handle PowerShell's space escape character correctly · Issue #33584 · microsoft/vscode
August 31, 2017 - PowerShell supports back tick (`) as a space escape character. Using this when creating a terminal command for PowerShell makes the terminal fail. For example dir folder with spaces The command that is constructed starting the terminal is:
Author: microsoft
🌐
Naga Learnings
pnagarjuna.wordpress.com › 2017 › 09 › 01 › escape-space-in-folder-path-in-powershell-scripts
Escape space in folder path in Powershell scripts – Naga Learnings
September 1, 2017 - Issue:child script path in .ps1 ... with errors as PS will recognize this path till first space. Fix: ` character is the escape character for spaces in PS....