Path unable to split

Himanshuk 161 Reputation points
2022-04-26T15:43:27.223+00:00

$a=(gwmi win32_service|?{ $_.name -eq "service name"}).pathname
$b=Split $a
Where I got error
I get path as $a="c\temp\mysvc.exe" I want only path i.e c:\temp without filename and I have tried many commands but get error that c drive not found in $b while split, how to extract path
If instead gwmi I hard code only path then it it split out but using gwmi pathname not getting split

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,574 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Andreas Baumgarten 111.5K Reputation points MVP
    2022-04-26T17:12:41.4+00:00

    Hi anonymous user ,

    you can try this:

    $a=(gwmi win32_service|?{ $_.name -eq "service name"}).pathname  
    Split-Path -Path $a  
    

    Or all in one line:

    $a = (gwmi win32_service|?{ $_.name -eq "servicename"}).pathname | Split-Path  
      
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Rich Matheisen 47,306 Reputation points
    2022-04-26T18:53:49.493+00:00

    Where have you defined the function named "Split"? Or have you assigned the name "Split" as an alias for the Split-Path cmdlet????

    If the value of the "pathname" property is "c\temp\mysvc.exe" (as you say in your post) then either you've omitted the colon (:) that should follow the drive letter, or the information is incorrect in the registry (i.e., you used the wrong path when you created the service).

    This, for example, returns "C:\Windows\system32" from the value in $a ("C:\WINDOWS\system32\svchost.exe -k AarSvcGroup -p"):

    $ServiceName = "AarSvc_2cb70ca6"
    $a = (Get-WmiObject -Query "select * from win32_service where name='$ServiceName'").pathname
    $b = Split-Path -Path $a -Parent
    
    0 comments No comments

  3. Himanshuk 161 Reputation points
    2022-04-27T07:47:36.177+00:00

    There is double quote prob in output for path In $b I am getting path as "c:\program files\Appdynamics\Appdynamics .net agent And the " is missing at the end @Rich Matheisen @Andreas Baumgarten See the following output ![196892-img-20220427-1319312.jpg][1] [1]: /api/attachments/196892-img-20220427-1319312.jpg?platform=QnA


  4. Andreas Baumgarten 111.5K Reputation points MVP
    2022-04-27T08:19:44.927+00:00

    Hi anonymous user ,

    please try this:

    $b = (Get-WmiObject win32_service | Where-Object { $_.name -eq "VMUSBArbService" }).pathname | Split-Path  
    $b = $b.Replace('"', '') # removes the leading "  
    $b = '"' + $b + '"' # adds the " at the beginning and the end  
    $b  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  5. Himanshuk 161 Reputation points
    2022-04-27T10:18:12.47+00:00

    @Andreas Baumgarten I have done and raised the PR yesterday only but this is the bug and not as permenent fix, say for 10,000 VMS will not recommend the patch work and have to find new workaround ![196982-img-20220427-1549002.jpg][1] [1]: /api/attachments/196982-img-20220427-1549002.jpg?platform=QnA


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.