0

I'm question.

I get process name througth powershell script using c#. But, i did not get value because process name contains whitespace.

How can I get value to contains whitespace in powershell using c#?

oops
  • 1
  • 2
  • Your `scriptText` is not valid PowerShell. What does `s = Get-Process | ?{$_.Id -eq '21952'}` mean? Variables have `$` in front of them. Where are the semicolons (`;`) between statements? – Jacob Krall Jan 16 '18 at 15:30
  • On my machine, `Get-Process` takes an `-Id` parameter, so you could simplify the assignment to `$s = Get-Process -Id 21952` – Jacob Krall Jan 16 '18 at 15:35

2 Answers2

0

You don't need PowerShell to get processes.

This sample line gives you the ID/Name for all processes with ID 21952 (although I suspect you won't have one with ID 21952).

 var processes = System.Diagnostics.Process.GetProcesses().Where(p => p.Id == 21952).ToDictionary(p => p.Id, p => p.ProcessName);

To remove that filter:

 var processes = System.Diagnostics.Process.GetProcesses().ToDictionary(p => p.Id, p => p.ProcessName);
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40
0

You could do the following directly in C#

using System.Diagnostics;
...
int pid = 21952;
Process localById = Process.GetProcessById(pid);

Console.WriteLine($"Process name for pid {pid}: '{localById.ProcessName}' '{localById.MainModule.FileName}'");
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54