1

I just want to determine the working directory of a running process. In Linux you can use pwdx, but i cant find a similar tool in windows. It would be perfect to get a commandline solution.

I tried nearly every possible command for windows. wmic gwmic taskkill.. none of them is a solution for my problem.

Enes
  • 33
  • 1
  • 4
  • 1
    Here's a [similar question](https://stackoverflow.com/q/20576834/243245), although it only looks like one of the answers is useful: [tlist from the WDK](https://stackoverflow.com/a/25853120/243245) will print the current directory, amongst other things. – Rup May 21 '19 at 17:16
  • 1
    What problem are you solving that you need the working directory of the process? – Bill_Stewart May 21 '19 at 19:51
  • Working directory or the path to a binary? I don't know that it's possible to retrieve the first. – FoxDeploy May 21 '19 at 21:33
  • 3
    The `CurrentDirectory` field of [`_RTL_USER_PROCESS_PARAMETERS`](https://learn.microsoft.com/en-us/windows/desktop/api/winternl/ns-winternl-_rtl_user_process_parameters) is undocumented. It's available in or to a debugger using the public symbols, but there's no public API to query this as far as I know. There's no reason another program or script would generally need this information. – Eryk Sun May 22 '19 at 02:19
  • @rup, i need a solution that is already installed on windows. – Enes May 22 '19 at 10:22
  • @Bill_Stewart my goal is to restart java-processes. Therefore i have to get the absolute Path to the jars in the execution command. – Enes May 22 '19 at 10:23
  • 1
    But any absolute paths in the command line would be relative to the directory the process was started from, which isn't necessarily the same as the current working directory which can change at runtime. So I'm not convinced that is what you need. Maybe [the handle list](https://stackoverflow.com/a/20592538/243245) for each process then, if Java holds open handles to its .jars? (I'm not sure it needs to.) – Rup May 22 '19 at 10:27
  • 1
    "i have to get the absolute Path to the jars in the execution command" - I would recommend specifying the absolute paths to the jar files in the Java command lines, and then you can use the WMI `CommandLine` property in the `Win32_Process` object. – Bill_Stewart May 22 '19 at 19:41

2 Answers2

1

The Get-Process command in PowerShell gives you the equivalent information (at least the path of the .exe)

>Get-Process ssms | Select -Expand Path | Split-path
 C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio

You could make this your own function, if you wanted:

Function pwdx{
    param($Process)
    Get-Process $Process | Select -Expand Path |Split-Path
 }

 C:\Users\FoxDeploy> pwdx notepad
 C:\WINDOWS\system32

If you need to do this in the command line in Windows, you can find the process this way.

wmic process where "name like '%notepad%'" get ExecutablePath
ExecutablePath
C:\WINDOWS\system32\notepad.exe
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • 1
    These show the process command line, not the working directory of the process. – Bill_Stewart May 21 '19 at 19:51
  • 2
    Actually your answer doesn't address the question at all..the OP is asking about how to get the _working directory for the process_, not the path to the executable. (Why the OP needs this is a separate question - I asked this in a comment.) – Bill_Stewart May 21 '19 at 21:25
  • With your solution i get the absolute path to the executable, not the working directory – Enes May 22 '19 at 10:26
0

If you want to receive the same information as the Process Explorer you can use a Windows Management Instrumentation query on the process.

Function Query-Process-WMI
{
    param
    (
        [Parameter( Mandatory = $true )]
        [String]
        $processName
    )

    $process                = Get-Process $processName
    $processInfo            = $process     | ForEach-Object { Get-WmiObject Win32_Process -Filter "name = '$($_.MainModule.ModuleName)'" }
    $processInfoCommandLine = $processInfo | ForEach-Object { $_.CommandLine }
    return $processInfoCommandLine
}
Riley Carney
  • 804
  • 5
  • 18