PowerShell quotes for Start-Process ArgumentList - powershell

I'm trying to have the app tablacus explorer open a folder path. This works fine with the following formatting:
$ex = 'S:\Tools\explorer\TE64.exe'
Start-Process $ex -ArgumentList '"Tabs,Close other tabs" "Open,C:\Program Files"'
But I would really like to have the path in a variable ($dir = 'C:\Program Files'), and I can't seem to get the quotes right so it gets interpreted properly.
Thank you for your help.

I found two solutions for this on the MS Blog:
$Args = #"
"Tabs,Close other tabs" "Open,$dir"
"#
start $ex -ArgumentList $Args
or
start $ex -ArgumentList """Tabs,Close other tabs"" ""Open,$dir"""

I found sometimes you need another level of quotes.
In my case, I have to set variables in -Arguments /v, so I had to use \"" to do that.
Start-Process `
-FilePath "Installer.exe" `
-Arguments "/s /qn /v""SOME_PARAM1=\""STRING_IN_PARAM\"" SOME_PARAM2=\""STRING_IN_PARAM\"""
-Wait ;

If your parameters are constant strings then create a shortcut and call that instead.
Set the 'target' of the shortcut to:
"S:\Tools\explorer\TE64.exe" "Tabs,Close other tabs" "Open,C:\Program Files"
Name your shortcut 'TE64' and call it in powershell like this:
start-process S:\Tools\explorer\TE64.lnk

The following syntax works fine for me, try this:
-ArgumentList "\`"$($variable)\`""

Related

Powershell: Start-Process with an argument

I want to start a setup.exe with one install parameter => /download example.xml
When I tpye in "C:\Temp\folder\setup.exe /download example.xml" in Windows Explorer Address Bar the setup.exe starts correctly.
How do I do that with Powershell?
I've tried the following:
$setup="C:\Temp\folder\setup.exe "
$Argument = "/download example.xml"
Start-Process $setup -ArgumentList $Argument
What am I doing wrong?
Thanks!
I think it wants a list of arguments. This might do the job
$setup="C:\Temp\folder\setup.exe "
$ArgumentLst = #("/download example.xml")
Start-Process $setup -ArgumentList $ArgumentLst

Passing arguments as a variable when installing an MSI using Start-Process

I am new to Powershell and, of course, trying to learn on the fly for a project- No pressure, right! :-)
I am working on a script to run an MSI package in quiet mode, passing it an activation code as an argument, that I have to extract from an XML file.
So far, I have everything working except for getting Start-Process to run the MSI with the arguments being passed in a Variable.
Set-ExecutionPolicy Bypass -Force
[System.Xml.XmlDocument]$XML_Doc = new-object System.Xml.XmlDocument
$XML_Doc.load('c:\myfolder\Configinfo.XML')
$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
write-host "Activation Id is: $ActivationID"
$InstallString = "`'/I C:\myfolder\myinstaller.msi akey="+'"'+$ActivationID+'"'''
#$InstallString = "`'/I C:\myfolder\myinstaller.msi akey=`"$($ActivationID)`"'"
write-host "$InstallString"'''
Start-Process msiexec.exe -ArgumentList $InstallString -Wait -NoNewWindow
#Start-Process msiexec.exe -ArgumentList '/I C:\myfolder\myinstaller.msi akey="12345678-abcd-1a1b-x9x1-a1b2c3d4e5f6"' -Wait -NoNewWindow
Above is the code I am working with now. The last line that is commented out is an activation string that works.
I have verified that $ActivationID is pulling back the correct value, and that $InstallString mirrors the argument list in the commented version of the Start-Process string.
Any help would be appreciated!
The Start-Process commands aren't necessary. PowerShell is a shell. It can run commands. Just put the commands you want to run directly in the script.
msiexec /i "C:\myfolder\myinstaller.msi" "AKEY=$ActivationID"
I quoted the parameters to msiexec.exe in case any of them contain spaces. PowerShell will automatically expand the $ActivationID variable into the string inside the double quotes.
Your ArgumentList is being passed incorrectly.
[Xml]$XML_Doc = Get-Content -Path 'C:\myfolder\Configinfo.xml'
$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
Write-Host "Activation Id is: $ActivationID"
$Path = 'msiexec'
$ArgList = #('/i','"C:\path\file.msi"',"akey=`"$ActivationID`"")
Write-Host "$Path $ArgList"
Start-Process -FilePath $Path -ArgumentList $ArgList -Wait -NoNewWindow
First off, let me welcome you to Powershell! It's a great language and a great community gathered around a common cause.
Since you're new to the language, you can still learn new tricks and that's a good thing, because it's generally accepted that the Write-Host cmdlet is nearly always a poor choice. If you don't trust me, you should trust the inventor of Powershell.
Now that that's out of the way, we should look at your MSI command. With Powershell, we don't have to directly open msiexec, and we can call the MSI directly. I would break the path to the installer into its own variable, and then we can add all of our arguments on top of it. Also, don't forget the "/qn" switch which will actually make all of this silent. All in all, your new script will look something like this:
[System.Xml.XmlDocument]$XML_Doc = new-object System.Xml.XmlDocument
$XML_Doc.load('c:\myfolder\Configinfo.XML')
$ActivationID = $XML_Doc.CONFIGINFO.SITEINFO.ACTIVATEID
Write-Verbose "Activation Id is: $ActivationID"
$msipath = "C:\myfolder\myinstaller.msi"
$args = #("akey=$ActivationID", "/qn")
Write-Verbose "Install path is $msipath"
Write-Verbose "Activation key is $akey"
Start-Process $msipath -ArgumentList $args -Wait -NoNewWindow

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"

Passing parameters between Powershell scripts

I simply want to be able to pass one parameter from one PS script to another, currently my script (script1) is as follows (all thanks to user CB):
$filepath = Resolve-Path "script2.ps1"
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`""
This succesfully opens another script in via another powershell instance. Now i want to be able to carry across a parameter to the 'script2.ps1' script. I have tried the following but this doesnt not work:
script1.ps1
$name = read-host "The name"
$filepath = Resolve-Path "script2.ps1"
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`"-name $name"
script2.ps1
Param(
[string]$name
)
write-host $name
This should simply pass over $name from script1 into $name in script2. I think im close but not quite close enough!
Thanks for any help!
The only problem I see is that you are missing a space after the last escaped ", try this:
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`" -name $name"
Is there a specific reason why you want to run the second script in a separate instance of Powershell? If not, you would do much better just to run the script directly:
$name = read-host "The name"
.\script2.ps1 -name $name
This way you don't have to worry about escaping any of the parameters.
The way you were doing it forces all of the parameters to be converted to strings and processed by Windows command line processing. That can be a nightmare to ensure values get through in a usable form. If instead you just invoke the script directly you can pass objects as parameters and Powershell is all about using objects rather than strings.

Executing a scriptblock via startprocess

Would any of you possibly know why this is not working?
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit -Command & `"{$outvar1 = 4+4; `"out: $outvar1`"}`"" -Wait
The ultimate purpose for this is so that i can run a script block as another user with the addition of the -Credential option. But i can not get this simple script block to work yet.
Many thanks.
Chris.
Here is somthing that is working:
PS C:\> Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `$outvar1`"}`"" -Wait
-ArgumentList is an array of strings
$outvar is interpreted so I use `$outvar