I want to save a PowerSell command in a variable and execute in a cmd Problem : get current path with exe file using command : $(get-location).Path
I try this but not working :
Set $path = powershell -Command $(get-location).Path
echo "$path"
Try for similar command line, Note we need to escape the first ) using ^)
C:\Users\K\Desktop>for /f "delims=" %c in ('powershell -Command $(get-location^).Path') do @set "$path=%c"
C:\Users\K\Desktop>echo %$path%
C:\Users\K\Desktop
C:\Users\K\Desktop>
so in a batch file use
for /f "delims=" %%c in ('powershell -Command $(get-location^).Path') do @set "$path=%%c"
echo %$path%
However there are many much simpler ways to find the current directory.
As suggested avoid any variable name that is very similar to "path" such as $path since it may be misunderstood by a downstream app stripping $, but the simplest way at cmd level would be to use the %cd% value:-
C:\Users\K\Desktop>set "ExDir=%cd%" & echo %ExDir%
C:\Users\K\Desktop
C:\Users\K\Desktop>
In a batch file it is common to use pushd and popd with a stored %~dp0 however at command line popd may not have a directory stack to return to, thus this method can be used.
C:\Users\K\Desktop>set "PopDir=%cd%"
C:\Users\K\Desktop>cd /d h:
H:\>echo doing somthing in %cd%
doing somthing in H:\
H:\>cd /D "%PopDir%"
C:\Users\K\Desktop>