How to get the current directory in the PowerShell cmdlet?

I am developing PowerShell 3.0 cmdlet using C # /. NET 4.0 in Visual Studio 2010. I want to get the current directory in PowerShell, where the user runs cmdlet . But Directory.GetCurrentDirectory () is not working properly. In the code below, the result is C: \ Users \ Administrator.

Question: What cmdlet code is used to get the current PowerShell directory?

 [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "StatusBar")] public class GetStatusBarCommand : System.Management.Automation.PSCmdlet { /// <summary> /// Provides a record-by-record processing functionality for the cmdlet. /// </summary> protected override void ProcessRecord() { this.WriteObject(Directory.GetCurrentDirectory()); return; } } 
+6
source share
1 answer

A PowerShell process can have multiple Runspaces, so one global catalog does not work for PowerShell. In addition, in PowerShell, your current directory may be in the registry, not the file system. However, you can access the file using the PowerShell API, for example:

 this.SessionState.Path.CurrentFileSystemLocation 
+20
source

All Articles