Skip to content

Instantly share code, notes, and snippets.

@toenuff
Last active December 24, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toenuff/6715996 to your computer and use it in GitHub Desktop.
Save toenuff/6715996 to your computer and use it in GitHub Desktop.
The following snippet gets the current directory whether or not it's run from a script or a PowerShell host. I put this at the top of nearly every script I write. Blogged as powerbit #9 here: http://powertoe.wordpress.com/2012/11/02/powerbits-9-set-the-current-directory-to-the-script-directory-or-the-current-directory-if-run-in-a-session/
# http://powertoe.wordpress.com/2012/11/02/powerbits-9-set-the-current-directory-to-the-script-directory-or-the-current-directory-if-run-in-a-session/
$currdir = ''
if ($MyInvocation.MyCommand.Path) {
$currdir = Split-Path $MyInvocation.MyCommand.Path
} else {
$currdir = $pwd -replace '^\S+::',''
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment