Tuesday, April 17, 2018

Get-Process cmdlet in PowerShell

In this article I’ll explain some cmdlets around how to get all active processes running on your computer and start/stop process using PowerShell.

You can get all active and running processes on your computer using below command.

Get-Process | more

You can specify names or ID of processes you want to get information about.

Get-Process -Name Chrome, explorer, powershell

Get-Process -Id 200,1692,7368,6988

You can use wildcard characters to search processes.

Get-Process -Name w*

You can specify local or remote computer name to get processes running on local or remote computer.

Get-Process -ComputerName Mitesh-PC -Name chrome,powershell

You can also use below command to check count of how many processes of single process is running using below command. You can use Group-Object cmdlets to group on property. Before you use group you need to sort data using Sort-Object cmdlet.

Get-Process | Sort-Object Name | Group-Object -Property Name

You can send result to output text file using Out-file cmdlets.

Get-Process | Sort-Object Name | Group-Object -Property Name | Select count, name | Out-File -FilePath C:\PowerShell\Processes.txt
Get-ChildItem process*.txt

You can find a specific process running on computer using where object and you can stop that processes same time via below cmdlets. In below example, I search for processes using CPU more than 50 and name starts with ‘A’ and stop all the processes matching this condition.

Get-Process | Where-Object {$_.CPU -ge 50 -and $_.Name -like 'A*'} | Stop-Process -PassThru


You can start process on your local computer using Start-Process cmdlets.

Start-Process notepad 

You can also specify additional parameters along with start-process.

Start-Process notepad -WindowStyle Maximized


You can stop any running processes using stop-process cmdlets.

Stop-Process -Name notepad
Stop-Process -ID 9964


This cmdlets wait for process to be stopped. If you execute this cmdlets for specific process, PowerShell prompt will waits until give process stopped manually.


This cmdlets debug one or more processes running on local computer. This cmdlets attaches the debugger to running process.

Get-Process powershell
Debug-Process powershell

I hope you have some knowledge about how to get processes and perform some actions around it using various PowerShell cmdlets.

Thank you for reading this article. Please leave your feedback below.

Reference –

See Also –

Monday, April 9, 2018

Mediator Pattern

Mediator design pattern promotes loose coupling between objects and communicate between them with the help of mediator object. Mediator Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –

Mediator pattern provides a way to communicate between objects indirectly via mediator object. The mediator object facilitates the communication between objects. When you have large number of objects in your application, which is very hard to maintain you should consider using Mediator pattern.  Mediator pattern avoids direct communication between objects and it uses mediator object that is responsible to communicate between source and destination object.

There are so many practical and real life examples available for Mediator Pattern. In this article, I have used simple Chat application example in which so many participants can send their messages to other participants via mediator.

Major components of Mediator pattern –

Client – This class creates an instance of multiple participants and sends messages between them.
Mediator – The Mediator interface for communicating with different participants (colleagues).
ConcreteMediator – This class implements the Mediator interface and maintain participants.
Participant (Colleague) class – This class has reference of Mediator object and uses this object to communicate with other participants.

Let’s have a look on below example of Mediator pattern.

Code –
public interface IChatRoom
    void Register(Participant participant);
    void SendMessage(string from, string to, string message);

//Concrete Mediator
public class ChatRoom : IChatRoom
    private Dictionary<string, Participant> participants =
        new Dictionary<string, Participant>();
    public void Register(Participant participant)
        if (!participants.ContainsValue(participant))
            participants.Add(participant.Name, participant);
        participant.ChatRoom = this;

    public void SendMessage(string from, string to, string message)
        if (participants.Keys.Contains(to))
            Participant participant = participants[to];
            participant.Receive(from, message);
            Console.WriteLine("Participant not registered.");
//Participant class (Colleague class)
public class Participant
    public Participant(string name)
        Name = name;
    public ChatRoom ChatRoom { get; set; }
    public string Name { get; set; }
    public void Send(string to, string message)
        ChatRoom.SendMessage(Name, to, message);
    public void Receive(string from, string message)
        Console.WriteLine("{0} to {1} - '{2}'",
            from, Name, message);
//Client class (entry point)
class Program
    static void Main(string[] args)
        ChatRoom chatroom = new ChatRoom();
        List<string> people = new List<string>()
        { "Mitesh", "Sandeep", "Vikram", "Amit" };
        List<Participant> participants = new List<Participant>();
        foreach (string s in people)
            participants.Add(new Participant(s));
        foreach (Participant p in participants)

        participants.Find(x => x.Name.Equals("Mitesh")).
            Send("Sandeep", "Hey Sandeep, How are you?");
        participants.Find(x => x.Name.Equals("Vikram")).
            Send("Mitesh", "Where are you?");
        participants.Find(x => x.Name.Equals("Sandeep")).
            Send("Mitesh", "Hi Mitesh, i am good. Thanks");
        participants.Find(x => x.Name.Equals("Mitesh")).
            Send("Vikram", "I am at home");
        participants.Find(x => x.Name.Equals("Amit")).
            Send("Vikram", "Hi");


Output –

As you can see in this example, Participant class has reference to mediator object (ChatRoom). ChatRoom object knows about different participants and facilitates communication between them. Client creates various participants and register them with mediator object and sends messages between different participants. The sender participant does not have direct reference to receiver participant and it communicates via Mediator object.

You can download full code from Gist.

I hope this article helps you to know more about Mediator Design Pattern. Please leave your feedback in comments section below.

References –

See Also –

Saturday, March 17, 2018

Advanced Files and Folders techniques with PowerShell

In my previous article, I explained basic files and folder PowerShell cmdlets. In this article I’ll show some advanced features and techniques to work with Files and Folders using PowerShell.


When you execute Get-ChildItem cmdlets it will display only default columns not all. You can use below command to check how many columns you can display with Get-ChildItem or any other command.

Get-ChildItem | Get-Member |more

The Get-Member command will list all the Methods and Properties of given command or object. If you want to get Properties of given command or object use below command.

Get-ChildItem | Get-Member -MemberType Properties

If you want to get only Methods of given command or object use below command.

Get-ChildItem | Get-Member -MemberType Method


As you know via Get-Member cmdlets you can find all the methods and properties of given command. So when you execute Get-ChildItem cmdlets it will display all the default columns but you can customize it via selecting specific columns to display. You can customize number of columns by specifying Select-Object or Select with pipe line.

Get-ChildItem -Path c:\Powershell | select Name, Length

You can also include other columns which are not displayed in default result. I included IsReadOnly, CreationTime, BaseName columns in below command.

Get-ChildItem | select Name, IsReadOnly, CreationTime, BaseName

You can also sort list of files and directories based on column names using Sort-Object or Sort cmdlets.

Get-ChildItem | select Name, IsReadOnly, CreationTime, Length | Sort Length, CreationTime -Descending 

You can use below command to count total files or folders recursively.

(Get-ChildItem -File -Recurse).count
(Get-ChildItem -Directory -Recurse).count

You can use Where cmdlets to search files or folders on specific conditions.

Get-ChildItem -file | where {$_.Name -eq "app.pdf" -or $_.Name -eq "Test.doc"} | select Name, Length, LastWriteTime | sort Length -Descending

You can use below command to get list of all the specified files and rename it via ForEach object.

Get-ChildItem -File -Filter *.PDF | foreach {Rename-Item $_.FullName -NewName ($_.BaseName + 1 + $_.Extension)}
Get-ChildItem -File -Filter *.PDF

The similar way you can use cmdlets like Move-Item, Copy-Item, Remove-Item, New-Item etc. along with Get-ChildItem to get list of files and folders and execute cmdlets on the fly.

I hope you have now some understanding on advanced techniques to list files and folders, selecting extra columns, sorting and counting total files and folders and loop through each files in foreach loop to perform some actions.

Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See Also –

Friday, March 2, 2018

Working with Files and Folders using PowerShell

In this article I’ll explain various PowerShell commands to perform File/Directory operations.


You can use ‘Get-ChildItem’ command to get list of all the files and folder of given path.

Get-ChildItem -Path C:\PowerShell 

You can use below commands to list either files or folders only of given path

Get-ChildItem -Path C:\PowerShell -File

Get-ChildItem -Path C:\PowerShell -Directory

You can use below command to filter files and folders as per your requirement.

Get-ChildItem -Path C:\PowerShell -Filter *.pdf

You can you below command to list all the files and folders of give path recursively.

Get-ChildItem -Path C:\PowerShell -Recurse

You can also use Include or Exclude parameters with Get-ChildItem command to filter files.

Get-ChildItem -Path C:\PowerShell -Include "*.pdf" -Exclude "s*.PDF" –Recurse

You can use ‘New-Item’ command to create new file or directory.

New-Item -Type File "C:\PowerShell\First1.txt"

New-Item -Type Directory "C:\PowerShell\MyFolder"


You can use Copy-Item command to copy files and directories.

Copy-Item First1.txt Mitesh\First2.txt
Get-ChildItem -Path C:\PowerShell\Mitesh


You can use Remove-Item command to delete files and directories.

Remove-Item c:\PowerShell\Mitesh\First2.txt
Get-ChildItem -Path C:\PowerShell\Mitesh


You can use ‘Rename-Item’ command to rename file and directories.

Rename-Item First1.txt First3.txt
Get-ChildItem -Path C:\PowerShell\Mitesh

You can use ‘Move-Item’ command to move files and directories.

Move-Item c:\PowerShell\First3.txt C:\PowerShell\Mitesh\First1.txt
Get-ChildItem C:\PowerShell\Mitesh


List files and folders
Create new files and folders
Copy files and folders
Delete files and folders
Rename files and folders
Move files and folders

I hope you now have some basic understanding about PowerShell file and folder cmdlets to play around it. 

Thank you for reading this article. Please leave your feedback in comments below.

Reference –

See Also –