0

The following code worked perfectly-

$ScriptLocation = Get-Location
Set-Location "$ScriptLocation"

...until I created a batch file to kick the script off. I understand that by opening the script via the batch file the location will be the root directory.

My question is, how can I set the current directory to the directory where the script is when launching the script from a batch file?

My batch file has the following code-

@ECHO OFF
SET ScriptDirectory=%~dp0
SET ScriptPath=%ScriptDirectory%FilePush_V0.1.ps1 
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""%ScriptPath%""' -Verb RunAs}";
Brady
  • 403
  • 1
  • 8
  • 19
  • I'm guessing there's something wildly wrong with this question since it was voted down? – Brady Jul 10 '14 at 21:43
  • 2
    See [whats-the-best-way-to-determine-the-location-of-the-current-powershell-script][1] [1]: http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script set-Location $PSScriptRoot cd.. – Bin Jul 10 '14 at 22:11
  • That seems to work, thank you. How about setting it to go up one directory afterwards? – Brady Jul 10 '14 at 23:18

1 Answers1

0
set-Location $PSScriptRoot
cd..

See the following for your version of powershell.

whats-the-best-way-to-determine-the-location-of-the-current-powershell-script

Community
  • 1
  • 1
Bin
  • 211
  • 1
  • 7
  • This wouldn't work for me on PS v2. I ended up using -`$ScriptLocation = Split-Path -Parent$MyInvocation.MyCommand.Definition` -`$ScriptRunPath = Split-Path -Path $ScriptLocation -Parent` -`Set-Location "$ScriptRunPath"` Ugh, how do I add line breaks? Double spaces don't work. – Brady Jul 11 '14 at 14:23