locked
How to take the path for a sevice/executable and then use the path to set-location? RRS feed

  • Question

  • Hi all,

    Looking for an alternative to check the path of one Service, store it in one variable and then use it to set the location for next commands, basically to have the script working on Windows 32 and 64 bits and considering installation in C, D, E or another drive unit.

    I´m using:

    $a = Get-WmiObject Win32_service | ?{$_.Name -eq "TeamViewer"} | select Pathname

    $a

    Pathname                                                  
    --------                                                  
    "C:\Program Files (x86)\TeamViewer\TeamViewer_Service.exe"

    Thanks in advance, any clue will be appreciated.

    Best Regards,

    Tuesday, October 16, 2018 8:20 PM

Answers

  • How to change to the directory where an installed service is running from:


    $svcExecName = Get-WmiObject Win32_Service -Filter "Name='TeamViewer'" |
        Select-Object -ExpandProperty PathName |
        Select-String '^"?(.+\.exe)"?' | ForEach-Object {
          $_.Matches[0].Groups[1].Value
        }
    Set-Location (Split-Path $svcExecName -Parent)
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by cristianom1 Friday, October 19, 2018 1:48 PM
    Thursday, October 18, 2018 2:23 PM

All replies

  • $a = Get-WmiObject Win32_service -Filter "Name='TeamViewer'"} | select -Expand Pathname

    \_(ツ)_/

    Tuesday, October 16, 2018 8:48 PM
  • Thank you for your attention,

    I´ve made a few adjustment, but it returns the same output, i need to take only the path without filename, so i can use it to set-location and continue working in this path  "C:\Program Files (x86)\TeamViewer"

    Get-WmiObject Win32_service -Filter {Name= "TeamViewer"} | select -Expand Pathname

    "C:\Program Files (x86)\TeamViewer\TeamViewer_Service.exe"


    • Edited by cristianom1 Tuesday, October 16, 2018 9:21 PM
    Tuesday, October 16, 2018 9:20 PM
  • help split-path -online


    \_(ツ)_/

    Tuesday, October 16, 2018 10:45 PM
  • Something like this?
     Get-WmiObject Win32_service -Filter {Name= "TeamViewer"} | select -Expand Pathname|%{split-path $_}


    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere

    Tuesday, October 16, 2018 10:46 PM
  • $a = Get-WmiObject Win32_service -Filter "Name='TeamViewer'"} | select -Expand Pathname | Split-Path

    \_(ツ)_/

    Tuesday, October 16, 2018 10:48 PM
  • Hi Gastone,

    Thank you for your attention, we are almost there, the command brings the path I desired, the only thing missing is the closing " without it is not possible to take this output to use as path:

    PS C:\> Get-WmiObject Win32_service -Filter {Name= "TeamViewer"} | select -Expand Pathname|%{split-path $_}
    "C:\Program Files (x86)\TeamViewer

    Only missing the closing " which would make it = "C:\Program Files (x86)\TeamViewer"

    Thank you again and please share this step if you have it.

    Wednesday, October 17, 2018 12:47 PM
  • There are no quotes in PowerShell.  A string is a string. The following returns exactly what you are asking for.

    Get-WmiObject Win32_service -Filter "Name='TeamViewer'" | select -Expand Pathname | Split-Path


    \_(ツ)_/

    Wednesday, October 17, 2018 4:45 PM
  • Probably this:

    (Get-WmiObject Win32_service  -Filter {Name= "TeamViewer"}  | select -Expand Pathname|split-path) -replace '"'



    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere



    Wednesday, October 17, 2018 7:29 PM
  • Probly this:

    (Get-WmiObject Win32_service  -Filter {Name= "TeamViewer"}  | select -Expand Pathname|split-path) -replace '"'



    Gastone Canali >http://www.armadillo.it


    Se alcuni post rispondono al tuo quesito(non necessariamente i miei), ricorda di contrassegnarli come risposta e non dimenticare di contrassegnare anche i post utili. GRAZIE! Ricorda di dare un occhio ai link Click Here andHere



    There are no quotes in a pathname.  Quotes are not allowed. The OP is doing something odd.

    \_(ツ)_/

    Wednesday, October 17, 2018 7:42 PM
  • The Pathname property of the Win32_Service instance might contain a quoted path.

    Here's one way to extract the executable name using a regular expression:


    Get-WmiObject Win32_Service -Filter "Name='TeamViewer'" |
        Select-Object -ExpandProperty PathName |
        Select-String '^"?(.+\.exe)"?' | ForEach-Object {
          $_.Matches[0].Groups[1].Value
        }
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 17, 2018 8:57 PM
  • How to change to the directory where an installed service is running from:


    $svcExecName = Get-WmiObject Win32_Service -Filter "Name='TeamViewer'" |
        Select-Object -ExpandProperty PathName |
        Select-String '^"?(.+\.exe)"?' | ForEach-Object {
          $_.Matches[0].Groups[1].Value
        }
    Set-Location (Split-Path $svcExecName -Parent)
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by cristianom1 Friday, October 19, 2018 1:48 PM
    Thursday, October 18, 2018 2:23 PM
  • Get-WmiObject Win32_Service -Filter "Name='TeamViewer'" |
        Select-Object -ExpandProperty PathName | % {
        (Get-Item ($_ -replace "`"","")).Directory.FullName}
    This will get you the same thing but Bills example above will work if there is a trailing command switch so it is the better option. Also Regexs are cool.
    Friday, October 19, 2018 12:37 AM
  • Get-WmiObject Win32_Service -Filter "Name='TeamViewer'" |
        Select-Object -ExpandProperty PathName | % {
        (Get-Item ($_ -replace "`"","")).Directory.FullName}
    This will get you the same thing but Bills example above will work if there is a trailing command switch so it is the better option. Also Regexs are cool.

    Notification:

    "replace" does not need a blank string....

    -replace "something",""

    is the same as:

    -replace 'something'

    Extra credit info:

    If you are not expanding a variable do not use " use '.  This prevents the possible "unintended consequences" result.

    Happy coding.


    \_(ツ)_/

    Friday, October 19, 2018 12:45 AM
  • Wow, I did not realize that was the default behavior of -replace. Cheers dude.
    Friday, October 19, 2018 12:48 AM
  • Always question everything. You will find that there are thousands of neat are available when coding.


    \_(ツ)_/


    • Edited by jrv Friday, October 19, 2018 12:53 AM
    Friday, October 19, 2018 12:52 AM
  • Hi Bill,

    It´s simply perfect.

    I just did not get this part

    Select-String '^"?(.+\.exe)"?' | ForEach-Object {
          $_.Matches[0].Groups[1].Value
        }

    Thank you and thanks for everyone helping in this fórum, it´s great to see people whith higher level of knowledge and desire to help.

    Friday, October 19, 2018 1:51 PM