Friday, September 12, 2008

PowerShell Tip: Getting the path of a directory as an ordinary string

If you using PowerShell, and ever need to get the full path of the present working directory for use in your script later as a string, use the command Convert-Path as shown in the example below:

PS C:\code> $myVar = Convert-Path .

This command sets $myVar to "C:\code" which in my case is useful for building a fully-qualified file reference later in my script.

It took me a lot of wrangling with Get-Item (which returns a PowerShell entity) and [System.IO.Directory] method calls (which I am still figuring out) to stumble on this one simple command, and I hope that this saves someone out there a couple of minutes!

1 comment:

Stephen Mills said...

You could also use:
$MyVar = $pwd.Path
$MyVar = (Get-Location).Path
$MyVar = (Get-Item .).FullName