How to handle spaces in external process - powershell

I am trying to start a powershell process with spaces, see the below examples:
Working:
$path = "C:\Windows\System32\notepad.exe"
Start-Process -FilePath powershell.exe -ArgumentList "& $path"
Not working (with spaces):
$path = "C:\Program Files (x86)\CustomApp\Server.exe"
Start-Process -FilePath powershell.exe -ArgumentList "& $path; Read-Host"
Using Read-Host the error message is displayed before the console exits: The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program.
How can I handle the spaces with Start-Process?

Place your path also in single quotes:
$path = "'C:\Program Files (x86)\CustomApp\Server.exe'"
Start-Process -FilePath powershell.exe -ArgumentList "& $path; Read-Host"

Related

How to escape space in PowerShell?

I run this command in PowerShell
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' -ArgumentList 'C:\Users\Administrator\Desktop\Hello world.md'
But Typora says
C:\User\Administrator\world.md does not exist
It looks like PowerShell executes
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe'
-ArgumentList 'C:\Users\Administrator\Desktop\Hello' 'world.md'
I want to escape the space but the single-quoted not working...
PowerShell version: 5.1.19041.610
PowerShell may not quote strings properly when calling external executables. See
Curl in PowerShell with custom cookie file
PowerShell or CMD errors on space in file path
So to fix this you need to pass the literal " to the exe file by escaping it properly
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' `
-ArgumentList '"C:\Users\Administrator\Desktop\Hello world.md"'
Try adding ` to excape spaces in powershell:
Start-Process -FilePath 'C:\Program Files\Typora\Typora.exe' -ArgumentList 'C:\Users\Administrator\Desktop\Hello`world.md'

Powershell-Command to download a file with admin rights

I try do download a file via a powershell command. The command I use is simple:
Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'
I can run the command in PS succesfully. But now I want to run it with elevated rights. So I gooogled and found this solution:
There it says the command should be:
Start-Process powershell.exe -Verb Runas -ArgumentList "-Command & {get-process}"
So I tried adjusting it for my use case:
Start-Process powershell.exe -Verb Runas -ArgumentList "-Command & {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'}"
But all is does is open a new PS-Window and closing it right after. Where is my mistake?
You can change to this
Start-Process powershell.exe -Verb Runas -ArgumentList "& {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'}"
Note the window will close after the execution completes. If you would like to see the output/errors (such as what would be shown in your non working example) just add another command to pause.
Start-Process powershell.exe -Verb Runas -ArgumentList "& {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip';pause}"
& is used to invoke a command. It's useful for executing strings or scriptblocks. It runs in a child runspace.
& 'Get-Host'
& 'Write-Host' Hello -Fore Green
& {Write-Host Goodbye -Fore Cyan}
; is used to separate different commands on the same line.
& {$name = 'Doug';Write-Host Hello $name}
You can also use a period to invoke a scriptblock in the current runspace. In the previous command the $name variable would be empty in the callers scope where the following command would leave the variable defined.
& {$name = 'Doug';Write-Host Hello $name}
$name # empty as it all happens in the child scope
vs
. {$name = 'Doug';Write-Host Hello $name}
$name # populated because it's brought into the caller's scope

PowerShell - How to use 'Start-Process' and rename the newly launched windowtitle

I am trying to use the PowerShell (version 2) Start-Process and rename the newly launched window title. I have the following snippet of code that works fine, it launches a new PowerShell window and tails logs ($c contains the log file to tail):-
Start-Process powershell.exe -Argument "Get-Content", $c, "-wait"
I am not sure how to include the following so that the newly launched window title can be changed.
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Cheers.
A "dirty" solution would be:
start-process powershell.exe -argument "`$host.ui.RawUI.WindowTitle = 'New window title rename example text'; get-content -Path $c -wait"
I would recommend creating a script for you commands and use parameters for input.
Untitled2.ps1
param($c)
$host.ui.RawUI.WindowTitle = 'New window title rename example text'
Get-Content -Path $c -Wait
Script
$script = Get-Item ".\Desktop\Untitled2.ps1"
$c = Get-Item ".\Desktop\t.txt"
Start-Process powershell.exe -ArgumentList "-File $($script.FullName) -c $($c.FullName)"
A cleaner powershell solution, especially if you're going to run further commands at the time you spawn the process, might be like this.
$StartInfo = new-object System.Diagnostics.ProcessStartInfo
$StartInfo.FileName = "$pshome\powershell.exe"
$StartInfo.Arguments = "-NoExit -Command `$Host.UI.RawUI.WindowTitle=`'Your Title Here`'"
[System.Diagnostics.Process]::Start($StartInfo)
if anyone need a easy way ,you can try this in powershell:
cmd.exe /c "start ""my app"" powershell.exe -NoExit -Command ""dotnet myapp"""
it also means this command in cmd:
start "myapp" powershell.exe -NoExit -Command "dotnet myapp"

Doesn't work execute arguments

I write powershell script, which extract files from sharepoint installation. When I execute this script, I see fileDialog, in which I need choose path.
$path="C:\Script\Download\SharePointServer.exe "
$arguments = "/extract "
start-process $path $arguments -wait -verb runas
But when I execute this script, I see error: Usage: /extract:path, extract the content of the package...
$path="C:\Script\Download\SharePointServer.exe "
$arguments = "/extract ""C:\Script\Download"""
start-process $path $arguments -wait -verb runas
So how it's fix?
You are almost there:
$path="C:\Script\Download\SharePointServer.exe"
$arguments = "/extract C:\Script\Download"
& $path $arguments;
Hope it works ;)

Powershell running as a another user with elevated privileges

I have two scripts located in C:\setup: script.ps1 and script1.ps1.
I want to be able to run the script1.ps1 from withing script.ps1 as another user and with elevated privileges but I cannot make it work. The new powershell window opens but closes immediately ...
here is the script:
$cspath = $MyInvocation.MyCommand.Path
$sfolder = Split-Path $cspath
$spath = Join-Path $sfolder "\Script1.ps1"
$sa = "domain\user"
$sap = "userpassword"
$sasp = ConvertTo-SecureString -String $sap -AsPlainText -Force
$sac = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sa, $sasp
Start-Process $PSHOME\powershell.exe `
-Credential $sac `
-ArgumentList "-Command Start-Process $PSHOME\powershell.exe -ArgumentList `"'$spath'`" -Verb Runas" -Wait
Any help will be appreciated ...
It looks like you might need to adjust your parameters for powershell.exe. Instead of using -ArgumentList, which I don't think is valid, you should use the -File parameter. Also, you will want to use the -ExecutionPolicy Bypass parameter to ensure that the script execution policy is not interfering.
Finally, I would recommend removing the single quotes from around the script path, as the Windows command interpreter does not understand single quotes to surround parameters.
Give this a try:
$ArgumentList = '-Command Start-Process -FilePath $PSHOME\powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File \"{0}\"" -Verb Runas' -f $sPath;
Start-Process $PSHOME\powershell.exe `
-Credential $sac `
-ArgumentList $ArgumentList -Wait
Update
It seems that some quoting rules were at play here as well, since we are embedding one command inside of another. I wrote and tested a fully function script on PowerShell v4.0.
Here are the contents:
# Create test directory and script file
[void](New-Item -Path c:\test -ItemType Directory -Force);
Set-Content -Path c:\test\test1.ps1 -Value 'Add-Content -Path $PSScriptRoot\blah.txt -Value (Get-Date);';
# Get credential and define script path
$Credential = Get-Credential;
$ScriptPath = 'c:\test\test1.ps1';
# Define the command line arguments
$ArgumentList = 'Start-Process -FilePath powershell.exe -ArgumentList \"-ExecutionPolicy Bypass -File "{0}"\" -Verb Runas' -f $ScriptPath;
Start-Process -FilePath powershell.exe `
-Credential $Credential `
-ArgumentList $ArgumentList -Wait -NoNewWindow;
I can confirm that I get a UAC prompt, and the target script successfully executes.
Since you're concerned about the new session window closing, I'm guessing you want command line output.
Start-Process is working as intended. It will run the script passed in through -ArgumentList and exit the session. This means it will not hold to display command line output - the session will terminate immediately after the process completes.
If you want a persistent session, use New-PSSession. Otherwise, you could export the data you're gathering to a file.