EDIT: This seems to be a pretty niche scenario that I'm running into, but the answer to the question itself is that powershell.exe inherits the working directory from the calling process. I.e. PUSHD does change the working directory of cmd and that is passed to PWSH.
As an aside, the issue I'm running into is that I don't have permissions and access to all of the folders in the working directory path. In particular below, I'm missing permissions for \Dev\. So if I locally map \\<ComputerName>\<ShareName>\<NoAccessVolume>\<AccessVolume> to a drive like my H:, it works fine since the mapping skips over <NoAccessVolume>. However, I can't simply have PowerShell set my location to \\<ComputerName>\<ShareName>\<NoAccessVolume>\<AccessVolume> because even though PowerShell can handle UNC paths, it will throw a permissions related error.
Without getting into why I'm using a batch file, I'm using one. As noted in another answer, batch files (.cmd, .bat) can be launched directly while .ps1 files cannot.
So here's the scenario:
- Saved my code as base64
- Created a
.batfile to invokepowershell.exeand pass the code using the-EncodedCommandparameter - Everything worked, until it didn't
One of the users either doesn't have their network share mapped to a local drive, or they went out of the way to access the network share using the UNC path. cmd.exe doesn't like this and it warns the us that the Windows Directory will be used. According to a different answer here on SO, %SystemRoot% is used. It is easy enough to use PUSHD and POPD to change the directory of cmd.exe (see various answers here) - However, powershell.exe doesn't seem to care about the temporary drive letter. So referencing $PWD within the script...not a good idea.
I can't find anything on about_PowerShell_exe that talks about the working directory - but it is pretty easy to say that if cmd.exe defaults to the Windows Directory, then powershell.exe will also use C:\Windows. However using PUSHD prior to calling powershell.exe results in PowerShell opening at C:\. So I guess I'm wondering what logic powershell.exe uses to determine the resulting PowerShell Working Directory and, in my specific case, if there are any easy options available to change the working directory to match %~dp0 (i.e. where .bat file was called from).
The first answer I linked (here as well) goes into some helpful detail on passing arguments to PowerShell when calling it from a batch file, so I know that is one route I can go - but is there anything hidden in powershell.exe that I'm missing?
EDIT: Here is what I'm getting currently. I am using Windows 10.
The contents of my batch file:
@echo OFF
echo Batch File: %0
powershell.exe -NoProfile -NoExit -Command Write-Host PowerShell Working Directory w/o PUSHD: $PWD
PUSHD "%~dp0"
powershell.exe -NoProfile -NoExit -Command Write-Host PowerShell Working Directory w/ PUSHD: $PWD
POPD
PAUSE
Running from a mapped drive; $PWD will work as expected:
Batch File: "H:\_Team Members\immobile2\test.bat"
PowerShell Working Directory w/o PUSHD: H:\_Team Members\immobile2
PS H:\_Team Members\immobile2> exit
PowerShell Working Directory w/ PUSHD: H:\_Team Members\immobile2
PS H:\_Team Members\immobile2> exit
Press any key to continue . . .
Running from the Network Share; $PWD will not work as expected
'\\dep-files01\Departments$\Dev\Data\_Team Members\immobile2'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Batch File: "\\dep-files01\Departments$\Dev\Data\_Team Members\immobile2\test.bat"
PowerShell Working Directory w/o PUSHD: C:\Windows
PS C:\Windows> exit
PowerShell Working Directory w/ PUSHD:
PS C:\> exit
Press any key to continue . . .