Question

I'm writing a powershell cmdlet. From inside of my cmdlet BeginProcessing() method, I want to be able to retrieve the the directory that was the default directory at the time the cmdlet was invoked.

Example: If the user does this:

cd \myDirectory
invoke-mycmdlet

I want for my code to know that the default shell directory was c:\myDirectory. When I access Environment.CurrentDirectory, it's always c:\windows\system32

I've seen a similar post on SO where the poster needed to set Environment::Current directory from inside the shell using get-location. That won't work for me.

Basically, my cmdlet does some file system stuff, and I want the user to be able to just cd\ into a directory, and execute my cmdlet, with it operating on the directory that they switched into -- just like you would expect it to work from the old Command Console.

Was it helpful?

Solution

You might want to try this instead, CurrentLocation.Path could also point to other provider paths, such as the registery.

this.SessionState.Path.CurrentFileSystemLocation.Path

OTHER TIPS

You know, I always seem to find it right after I post -- regardless of how long I spent looking before reaching for SO!

So, my cmdlet inherits from PsCmdlet. I found that I could get the path I wanted from

this.SessionState.Path.CurrentLocation.Path

(where "this" is a cmdlet class that inherits from PsCmdlet)

Try the Get-Location cmdlet. It should be the script's current executing location, rather than the powershell host startup folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top