Reputation: 287
I'm my PS script I want to be able to run another script in another PS instance by doing the following:
$filepath = Resolve-Path "destruct.ps1"
start-process powershell.exe "$filepath"
destruct.ps1
is in the same folder as this script.
However when running this script in a location which includes spaces ("C:\My Scripts\"
) I will get the following error:
The term 'C:\My' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
I know by using a '&'
with the Invoke-Expression method solves this problem, how can I do the same but by using the start-process method?
Upvotes: 13
Views: 46864
Reputation: 1
Example of Start-Process simple
$Path="C:\Program Files (x86)\New folder" # Path with spaces
$ScriptName='toto.ps1' # Script name
$ScriptArgument="-ExecutionMode $ExecutionMode" # Script arguments
# Spaces and other characters escaped
$ArgumentList="$($Path.Replace(' ','` ').replace('(','`(').replace(')','`)'))" + "\$ScriptName" + " " + $ScriptArgument
# Start-Process
Start-Process -FilePath powershell.exe -ArgumentList $ArgumentList -PassThru -Wait -WindowStyle Hidden -ErrorAction SilentlyContinue
Example of Start-Process with Runas
$Path="C:\Program Files (x86)\New folder" # Path with spaces
$ScriptName='toto.ps1' # Script name
$ScriptArgument="-ExecutionMode $ExecutionMode" # Script arguments
# Spaces and other characters escaped
$Path="$($Path.Replace(' ','` ').replace('(','`(').replace(')','`)'))"
# Start-Process
$Command="-Command Set-Location $Path;$Path\$ScriptName $ScriptArgument"
$Command="-Command Start-Process -FilePath Powershell.exe '$Command' -Verb RunAs -PassThru -Wait -WindowStyle Hidden"
Start-Process -FilePath powershell.exe $Command -PassThru -WindowStyle Hidden -ErrorAction SilentlyContinue
Upvotes: 0
Reputation: 1
I'm running Windows 10, Powershell Version 5.1. I was having the same issue with spaces in the file path when attempting to execute PS1 scripts. In my case, I had one script that was calling another in the same folder. The purpose of the first script is to elevate the second script to admin privileges. The spaces were in the user's name on the computer, so that their desktop was something like "C:\Users\User Space Name\Desktop". I tried every suggestion across the internet, including the suggestions here and on other StackOverflow posts. NONE worked. I could get the scripts to run if I opened Powershell first, but I could not get them to run by double-clicking the file. I would always get errors in the form of "The term 'C:\Users\User' is not a recognized cmdlet..."
I found out that if you edit the registry entry for the file extension association to powershell, it will start working. The registry entry is as follows:
HKEY_CLASSES_ROOT\Applications\powershell.exe\shell\open\command
Default Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "%1"
New Value: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "& "%1""
I'm not sure why this works but it does.
Upvotes: 0
Reputation: 2478
In case you came across this issue looking for a solution using PowerShell 7 (pwsh):
Basic example:
Start-Process pwsh -ArgumentList ('"path/to/my script with spaces.ps1"')
Example with an additional switch:
Start-Process pwsh -ArgumentList ('-NoExit "path/to/my script with spaces.ps1"')
Example running the script as an administrator:
Start-Process -Verb runAs pwsh -ArgumentList ('-NoExit "path/to/my script with spaces.ps1"')
This is a workaround for issue https://github.com/PowerShell/PowerShell/issues/5576 that hasn't been resolved in over 5 years.
Upvotes: 3
Reputation: 1
Just in case [string]$shipno (which is path & file name) comes in including spaces the following allows it to be passed to -FilePath successfully:
if ($shipno.contains(" ") -eq $true) {
$shipno = """" + $shipno + """"
}
Upvotes: 0
Reputation: 1176
File name might contain spaces, so preserve spaces in full path:
Notepad++ exec command:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"$(FULL_CURRENT_PATH)\""
same from command prompt:
"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe" "& \"C:\a_work\Systems\name with spaces.ps1\""
Upvotes: 0
Reputation: 21311
I am answering here for a general scenario.
If you need to navigate to a folder for example C:\Program Files
from the Powerhsell, the following command won't work as it has white space in between the path.
cd C:\Program Files
Instead embed the path with double quotes as like the below.
cd "C:\Program Files"
Upvotes: 2
Reputation: 68283
You can add escaped double quotes so that you pass a quoted argument:
"`"$filepath`""
Upvotes: 17
Reputation: 60928
try this:
start-process -FilePath powershell.exe -ArgumentList "-file `"$filepath`""
edit after comments:
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`""
side note:
$filepath
is a [pathinfo]
type and not a [string]
type.
Upvotes: 22