Well, this is a cmd.exe problem, but there are some ways to solve it.
Use single quotes
powershell test.ps1 -x 'hello world' -y 'my friend'Use the
-fileargumentpowershell -file test.ps1 -x "hello world" -y "my friend"Create a
.batwrapper with the following content@rem test.bat @powershell -file test.ps1 %1 %2 %3 %4And then call it:
test.bat -x "hello world" -y "my friend"
Well, this is a cmd.exe problem, but there are some ways to solve it.
Use single quotes
powershell test.ps1 -x 'hello world' -y 'my friend'Use the
-fileargumentpowershell -file test.ps1 -x "hello world" -y "my friend"Create a
.batwrapper with the following content@rem test.bat @powershell -file test.ps1 %1 %2 %3 %4And then call it:
test.bat -x "hello world" -y "my friend"
One can use a backtick ` to escape spaces:
PS & C:\Program` Files\\....
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
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.
Deadly-Bagel almost had it I think. Don't use double quotes in $outputpath to keep Get-Content happy, but add them in your argument list string.
Be sure to escape with backtick `. In fact you might just want to do that with all the paths:
$outputpath = 'C:\Users\bob\Desktop\output new.xml'
Start -FilePath $sqlpackage -ArgumentList "/action:DeployReport /SourceFile:`"$dacpacpath`" /Profile:`"$profilepath`" /OutputPath:`"$outputpath`"" -NoNewWindow -Wait
[xml]$xmldocument = Get-Content -Path $outputpath
-ArgumentList accepts String[] so try the following:
$outputpath = "C:\Users\bob\Desktop\output new.xml"
Start -FilePath $sqlpackage -ArgumentList @("/action:DeployReport", "/SourceFile:$dacpacpath", "/Profile:$profilepath", "/OutputPath:$outputpath") -NoNewWindow -Wait
I suspect passing it all as one parameter causes it to be confused.
I wrote a simple script to do a google search. Adding text from a textbox to the end of the URL for searching. It works, but not if there are multiple words with spaces. How can I replace the spaces with a + ?
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
Not tried it but maybe don’t replace the comma(,) at all.
Split on the comma first:
$lastname, $data = $l.Split(',')
then further split $data on spaces.
Scenario:
I’m given list of names from management and need to do an AD lookup for each name.
Names are in format:
Lastname, Firstname Dept and/or Title
Such as:
Freeman, Morgan DEV Supervisor
Here’s what I’ve got:
ForEach($l in $list){
$l = $l -replace '-' # drop hyphens
$l = $l -replace "'" # drop apostrophes
$l = $l -replace '[,]',' ' # replace commas with single-space
$l = $l -replace " "," " # replace triple-space with single-space
$l = $l -replace " "," " # replace double-space with single-space
}
$lastname = $l.Split(" ")[0]
$firstname = $l.Split(" ")[1]
$title = $l.Split(" ")[2]
This works for everything I’ve tried so far, EXCEPT people with spaces in their last names, such as:
Del Toro, Morgan DEV Supervisor
I’m really struggling with a regex statement that says something like “match only the FIRST space in a string but ONLY if it occurs before the first comma”
The closest thing I’ve tried so far is:
"Del Toro, Morgan DEV Supervisor" -replace '([^,]) (.*)', '$1$2'
That works with the spaced last name, but with a normal last name it deletes the space between “Morgan” and “DEV” instead.
I’ve tried several traditional regex patterns like ‘^\s,’ or such but am really stuck on this one.