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 –

Saturday, February 24, 2018

Introduction to Windows PowerShell

As name suggest, Windows PowerShell is powerful command line shell. Windows PowerShell is built on top of .Net framework and internally uses .Net framework objects to run commands. PowerShell uses cmdlets (“command-let”) to perform various actions. PowerShell ships with hundreds of cmdlets and you can also create your own cmdlets as per your need. Windows PowerShell is installed by default with Windows 7 SP1 onwards editions.

PowerShell is a tool and can be used to perform day to day activities. This tool is very useful to developers and administrators to do their activities and they can write scripts or programs to ease their work. Like many other shells, Windows PowerShell allows you to access file/directory system and other computer/network/remoting related information.

In this article, I’ll explain how to start PowerShell and introduce some basic commands for you to start with PowerShell.

How to start PowerShell

For windows 10 – search PowerShell on search bar. (Pre-installed with windows 10)

Once you launch PowerShell below window will appear.

You can use directory commands like DIR, CD, CD\, CD.., MKDIR, RMDIR, COPY, DEL etc with PowerShell that you already using with Command Prompt.

When you execute these commands on PowerShell, internally PowerShell uses other cmdlets. For example, when you type DIR command on PowerShell window, it will internally use Get-ChildItem command to list all directories and files.


You can use Get-Alias command to check which underlying command is mapped with alias.

Get-Alias dir

To check the entire alias you can type Get-Alias command without parameter. 



You can use ‘Get-Command’ to check what all commands available in PowerShell. You can use ‘|more’ to view details in page wise manner.

Get-Command | more

You can also search available command using wildcards like below.

Get-Command *process

You can also use verb or noun parameters to get all the commands with specified verb or noun

Get-Command -Verb get | more

Get-Command -noun process
You can use below command to check total commands currently available in PowerShell.



You can use Get-Help command to get help about any commands in PowerShell.

Get-Help Get-ChildItem | more

If you would like to know help only about specific parameter of that command then you can use below command.

Get-Help Get-ChildItem -Parameter filter

You can get help of any command in all the details with example using below command.

Get-Help Get-ChildItem -Full |more

If your PowerShell help is not updated since long time then you can use below command to update help. This command will take some time to execute and download help.

Update-Help -Force

I hope you now have some basic knowledge about how to start PowerShell and how to search various commands and get help about them. 

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

Reference –

Sunday, August 27, 2017

Iterator Pattern

Interpreter design pattern is used to access elements of collection sequentially. Iterator Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –

Iterator pattern provides a way to access elements of collection sequentially without exposing its representation. The client code should not be concerned about the actual implementation of traversing the elements of collection. The iterator pattern encapsulates such implementation and provides common way to traverse different types of collection.

Major components of Iterator pattern –

Client – This class creates an instance of collection object (ConcreteAggreate) and iterator object to perform next operation on collection to retrieve next items until all items are retrieved.

Iterator – The Iterator interface defines all the methods to traverse the collection.

ConcreteIterator – This class implements the Iterator interface and provides actual implementation of how the elements are traverse in collection.

Aggregate – This is an abstract class. This class defines method to create an Iterator.

ConcreteAggregate – This class will be derived from abstract class Aggregate and implements and creates an iterator object.

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

Code –

class Program
    static void Main(string[] args)
        //Create myObject collection and items to it.
        MyObjectCollection aggr = new MyObjectCollection();

        //Get Iterator and iterator all items
        Iterator itor = aggr.GetIterator();
        while (!itor.IsDone())
public interface Iterator
    object First();
    object Next();
    object CurrentItem();
    bool IsDone();
public class MyIterator : Iterator
    private MyObjectCollection aggregate;
    int index;
    public MyIterator(MyObjectCollection aggr)
        this.aggregate = aggr;
        index = 0;
    public object CurrentItem()
        return aggregate[index];
    public object First()
        return aggregate[0];
    public bool IsDone()
        return index >= aggregate.Count ;
    public object Next()
        object retVal = null;
        if (index < aggregate.Count)
            retVal = aggregate[index++];
        return retVal;
public abstract class Aggregate
    public abstract Iterator GetIterator();
public class MyObjectCollection : Aggregate
    private ArrayList myList = new ArrayList();

    public override Iterator GetIterator()
        return new MyIterator(this);
    public object this[int index]
        get { return myList[index]; }
    public int Count
        get { return myList.Count; }
    public void Add (object obj)

Output –

As you can see in this example, I created an instance of MyObjectCollection class and added few data items to it. I used ArrayList to add any object to the collection. You can use any type as per your requirement. You can also use generic type here. I get instance of iterator from MyObjectCollection instance and I’m iterating all the items of my collection using iterator instance. I’m using Iterator.Next() method to iterate to next item and Iterator.CurrentItem() to get current item of collection.

The Iterator patterns are available inbuilt with Microsoft .Net.  It provides two interfaces IEnumerable and IEnumerator. You can make your Iterator collection implementing IEnumerable interface. Almost all majorly used collections available in .Net implements IEnumerable interface. The few examples of inbuilt collections are List<T>, Dictionary<T>, ArrayList, SortedList etc. See below definition of both the interfaces.

public interface IEnumerable
        IEnumerator GetEnumerator();

public interface IEnumerator
        object Current { get; }
        bool MoveNext();
        void Reset();

Let’s have a look on below example of custom collection implementing IEnumerable interface.

class Program
    static void Main(string[] args)
        MyCollection coll = new MyCollection();
        foreach(var item in coll)


public class MyCollection : IEnumerable
    List<int> myList = new List<int>() { 2, 5, 3, 6, 4, 9 };
    public IEnumerator GetEnumerator()
        foreach (var item in myList)
            yield return item;

Output –

As per above code, IEnumerable provides GetEnumerator method which will return IEnumerator list. This list iterated through foreach loop in main class. The yield return keyword can be used to provide a value to the enumerator object.

You can download full code of Iterator Design Pattern from here. You can download code for Iterator Pattern implementing IEnumerable interface from here

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

References –

See Also –