Home > .net > Reading command line of another process

Reading command line of another process

September 24, 2009 Leave a comment Go to comments

Colleague Doug mentioned a need to be able to read the command line of another process from PowerShell.

My 1st attempt was to port Felix Kasza’s code into C# as I’d done back in my VBA days. Unfortunately, neither of our apps work in Vista x64.

I then switched to WMI and had much better luck (without pain as well). Further searching afterwards on how exactly the WMI team accomplished this led me to Raymond Chen’s blog.

namespace CmdLineReader {
    using System;
    using System.Management;

    class Program {
        static void Main( string[] args ) {
            try {
                if ( args == null || args.Length != 1 ) {
                    Console.WriteLine( "Please provide a process id." );
                string qry = string.Format( @"select CommandLine from Win32_Process
                                        where ProcessId={0}", args[ 0 ] );
                using ( var searcher = new ManagementObjectSearcher( qry ) ) {
                    using(ManagementObjectCollection coll = searcher.Get()) {
                        foreach(var o in coll)
                            Console.WriteLine("[{0}]", o["CommandLine"]);
            } catch ( Exception exc ) {
                Console.WriteLine( exc.ToString() );
Categories: .net Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: