Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8829237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:47:22+00:00 2026-06-14T07:47:22+00:00

It is my first time to post a question. I am both new to

  • 0

It is my first time to post a question. I am both new to csharp and powershell. Please bear with me if my english is not good at all.
I’ve been trying to convert my csharp code into powershell, but i’m not successful. I tried to research documents about powershell and try each one of them, unfortunately, nothing work for me. My csharp code is working in Visual Studio 2010 but I could not able to embed and work it in Powershell.
The code below simply catch PID of a taskmanager and write it to notepad with time and date stamp.
Any help will be greatly appreciated.

My csharp code is:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace GET_TASK
{
    class Program
    {
        static void Main(string[] args)
        {

            var processList = Process.GetProcessesByName("taskmgr");                

            foreach (var process in processList)
            {

                string myPath = "C:/powershell/GET_TASK"; //change data path as needed

                FileStream logfile;
                //log program process
                logfile = new FileStream(myPath + "/logfile.txt", FileMode.Append, FileAccess.Write); 

                TextWriter oldLog = Console.Out;  

                StreamWriter writelog;
                writelog = new StreamWriter(logfile);               

                DateTime time = DateTime.Now;
                string format = "MMM ddd d HH:mm yyyy";

                Console.SetOut(writelog);

                // print out the process id
                Console.WriteLine("Process Id=" + process.Id + " - " + time.ToString(format));

                Console.SetOut(oldLog);                        

                writelog.Close();

                System.Environment.Exit(0);                               
            }
        }
    }
}

In my Powershell code, I just put

$source = @"

at the begging of this code and

Add-Type -TypeDefinition $source
[GET_TASK.Program]::Main()

at the end of the code but it don’t work. I’m using Powershell version 1. Please help convert my C# code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:47:24+00:00Added an answer on June 14, 2026 at 7:47 am

    Try this:

    get-process | % { "Process ID = $($_.id) - $(get-date -Format "MMM ddd d HH:mm yyyy")" } |
    out-file -path "C:/powershell/GET_TASK" -append
    

    It’s the powershell equivalent code.

    Your code for using in add-type (only powershell V2 or V3, for V1 you have to create a DLL and use [Reflection.Assembly]::LoadFile("c:\myddl.dll") )

    $t = @"
    using System;
    using System.Diagnostics;
    using System.IO;
    
     public  class Program
        {
            public static void Main()
            {    
                var processList = Process.GetProcessesByName("taskmgr");    
                foreach (var process in processList)
                {    
                    string myPath = "C:/"; //change data path as needed    
                    FileStream logfile;
                    //log program process
                    logfile = new FileStream(myPath + "/logfile.txt", FileMode.Append, FileAccess.Write);    
                    TextWriter oldLog = Console.Out;    
                    StreamWriter writelog;
                    writelog = new StreamWriter(logfile);
                        DateTime time = DateTime.Now;
                    string format = "MMM ddd d HH:mm yyyy";    
                    Console.SetOut(writelog);
                        // print out the process id
                    Console.WriteLine("Process Id=" + process.Id + " - " + time.ToString(format));    
                    Console.SetOut(oldLog);    
                    writelog.Close();    
                    //System.Environment.Exit(0); removed, otherwise closes the console  
              }
            }
        }
    "@
    
    Add-Type -TypeDefinition $t
    [program]::main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time post here, I hope its a valid question. I've been building a
first of all ,forgive my poor english. this is the first time I post
This is my first time here so I hope I post this question at
I've been stuck on this for hours, first time post. I Don't know how
this is my first post on this website, but I'm all the time getting
This is my first time posting a question here, it has been a valuable
First time I post a question, so here it goes. I want to push
First time posting a question on StackOverflow, so please go easy on me :)
this is my first time to post a question here, I will try to
long time listener. First time caller... Not strictly a PHP question as it involves

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.