Mailing List



NerdyHearn - Blog

<< Back To All Blogs

Current file path in custom PowerShell Cmdlet

Wednesday, April 1st, 2009

As obvious as this may seem it is not very easy to find out how to obtain the current location of the filesystem from within a custom PowerShell Cmdlet. This is very necessary when attempting to write files to the current directory that is executing the cmdlet, saving files to the currect directory, etc.

First, you will need to make sure that you are inheritting from PSCmdlet and not the generic Cmdlet. Discussion on this is out of the scope of this entry, but basically in order to access Console-specific information in a Cmdlet (such as profiles, environment information, paths, etc) you need to inherit from PSCmdlet.

You can then access the current path (this will be where your PowerShell console is currently sitting, so say if you changed to C:Test it would give you that path) by using the property SessionState.Path.CurrentFileSystemLocation.Path.

Pretty simple now that I'm aware of it, but took me a little time to dig up.

Hopefully that will help some of you out.

PowerShellin' Tom Out.



Related Blogs

Programatically Retrieving an Assembly's PublicKeyToken through a PowerShell CmdLet
Customizing PowerShell for faster development with a default profile


Currently no comments.

Add A Comment



Email Address: (not public, used to send notifications on further comments)


Enter the text above, except for the 1st and last character:

NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers