Launching an application via PS

Welcome Forums General PowerShell Q&A Launching an application via PS

This topic contains 9 replies, has 3 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #133694

    Participant
    Points: 53
    Rank: Member

    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: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

    What should I be typing, please?

    Thanks!

  • #133697

    Participant
    Points: 328
    Helping Hand
    Rank: Contributor

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

    
    Get-Help Start-Process -Parameter filepath
    
    

    That will return the below results:

    -FilePath
    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.

  • #133700

    Participant
    Points: 53
    Rank: Member

    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.

  • #133712

    Participant
    Points: 328
    Helping Hand
    Rank: Contributor
    Get-Help about_Command_Syntax -ShowWindow
    

    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.

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

     

     

  • #133719

    Participant
    Points: 53
    Rank: Member

    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.

     

  • #133730

    Participant
    Points: 328
    Helping Hand
    Rank: Contributor

    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)"

    $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}}
    }
    
  • #133740

    Participant
    Points: 53
    Rank: Member

    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.

  • #133760

    Participant
    Points: 328
    Helping Hand
    Rank: Contributor

    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.

    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.

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

    Participant
    Points: 1,317
    Helping Hand
    Rank: Community Hero

    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

  • #133815

    Participant
    Points: 53
    Rank: Member

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

The topic ‘Launching an application via PS’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort