Launching an application via PS

Hello

I have been following this article about launching an application via PS:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

So, I tried this:

PS C:\> Start-Process -C:\ ProgramFiles \ "ImgBurn.exe"
and get the following error:

Start-Process : A positional parameter cannot be found that accepts argument ‘ImgBurn.exe’.
At line:1 char:1
+ Start-Process -C:\ ProgramFiles \ “ImgBurn.exe”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:slight_smile: [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

What should I be typing, please?

Thanks!

Using the “Get-Help” cmdlet would shed light on this.

[pre]

Get-Help Start-Process -Parameter filepath

[/pre]

That will return the below results:

-FilePath <String>
Specifies the optional path and file name of the program that runs in the process. Enter the name of an executable file or of a document, such as a .txt or .doc file, that is associated with a program on
the computer. This parameter is required.

If you specify only a file name, use the WorkingDirectory parameter to specify the path.

Required? true
Position? 0
Default value None
Accept pipeline input? False
Accept wildcard characters? false

 

As you can see -FilePath parameter is position 0 (which is why you don’t need to declare it) and it expects the full path to the file.

Thanks for that, Logan. It doesn’t seem to like the ’ \ ’ Do I use spaces as I did before:

C:\ ProgramFiles \ "ImgBurn.exe"
Thanks again.

[pre]
Get-Help about_Command_Syntax -ShowWindow
[/pre]

Search for “Parameter Values”:

Parameter Values

A parameter value is the input that the parameter takes. Because Windows
PowerShell is based on the Microsoft .NET Framework, parameter values are
represented in the syntax diagram by their .NET type.

For example, the Name parameter of Get-Help takes a String value, which
is a text string, such as a single word or multiple words enclosed in
quotation marks.

 

So based on the folder structure most commonly found on Windows OS the syntax would probably be something like.

[pre]

Start-Process -FilePath ‘C:\Program Files\imgBurn\imgburn.exe’

[/pre]

 

 

Thanks again. PS does not like this:

PS C:\> Start-Process -FilePath 'C:\Program Files\ImgBurn.exe'
or this:
PS C:\> Start-Process -FilePath 'C:\Program Files\imgBurn\imgburn.exe'
or this:
PS C:\> Start-Process -FilePath 'C:\Program Files (x86)\ImgBurn.exe'
Same error in each case: cannot find the file. It is on my C drive in the Program Files (x86) folder and is not in its own folder. The filename is called ImgBurn.exe (exactly like that).

But I am getting a grasp of what you mean, thanks.

 

That is odd, that would normally indicates there is an issue with the argument being passed to the -FilePath parameter. This would launch anything matching ‘imgburn.exe’ under 'C:\Program Files (x86)"

[pre]
$path = ‘C:\Program Files (x86)’
$filter = ‘imgburn.exe’
$fullName = Get-ChildItem -Path $path -Filter $filter -Recurse | Select-Object -ExpandProperty FullName
switch ($fullName)
{
{$fullName.count -eq 1} {Start-Process -FilePath $fullName}
{$fullName.count -lt 1} {Write-Output -InputObject ‘No files found’}
{$fullName.count -gt 1} {Write-Output -InputObject ‘Variable $fullname has multiple entries’;foreach ($name in $fullName){Start-Process -FilePath $name}}
}
[/pre]

It will take me some time to digest that code! This is what I got:

PS C:\> $path = 'C:\Program Files (x86)' >> $filter = '*imgburn*.exe' >> $fullName = Get-ChildItem -Path $path -Filter $filter -Recurse | Select-Object -ExpandProperty FullName >> switch ($fullName) >> { >> {$fullName.count -eq 1} {Start-Process -FilePath $fullName} >> {$fullName.count -lt 1} {Write-Output -InputObject 'No files found'} >> {$fullName.count -gt 1} {Write-Output -InputObject 'Variable $fullname has multiple entries';foreach ($name in $fullN ame){Start-Process -FilePath $name}} >> } Variable $fullname has multiple entries Variable $fullname has multiple entries PS C:\>
And then ImgBurn opened up on my desktop!

Many thanks again, Logan, for your time.

[quote quote=133740]It will take me some time to digest that code! This is what I got:

And then ImgBurn opened up on my desktop!

Many thanks again, Logan, for your time.

[/quote]
Very glad it launched it for you. If you wanted to see the full paths to the executable you could modify the last scriptblock in the Switch statement to output the fullname property. That would provide the exact path passed to Start-Process that launched the application. That would shed some insight into what was being done incorrectly in the original command when combined with what we now know about strings in PowerShell.

[pre]

{$fullName.count -gt 1} {Write-Output -InputObject $fullName}

[/pre]

This is a very common thing to do. Meaning starting .exe’s and fully documented by Microsoft.

PowerShell: Running Executables
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Thanks a lot, postanote. I will take a look at the link you have kindly posted.