How to get current path/directory in power shell in C#

This is a quick tutorial which explains how can you get the current directory of the power shell navigator. As an example:

Navigator Current Directory

PS C:\Windows\System32>

C:\Windows\System32

PS C:\Program Files\>

C:\Program Files

PS E:\MyFile>

E:\MyFile

This can be done in C# using this piece of code:

using System.Management.Automation;

// Declare the class as a cmdlet and specify an 
  // appropriate verb and noun for the cmdlet name.
  [Cmdlet("Get", "CurrentDirectory")]
  public class GetCurrentDirectoryCommand : Cmdlet
  {
    protected override void BeginProcessing()
    {
      // Write current path
      Console.WriteLine(SessionState.Path.CurrentLocation.Path);
    }
  }

One thought on “How to get current path/directory in power shell in C#

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