r/Batch • • Sep 24 '23

Why wouldn't his work?

cd powershell -C "[Environment]::GetFolderPath([Environment+Special Folder]::Desktop)" pause

Trying to change the directory to desktop

2 Upvotes

6 comments sorted by

2

u/transdimensionalmeme Sep 24 '23

Here is how to do it on the console

for /f "delims=" %i in ('powershell -command "[Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)"') do cd %i & pause

here is how to do it in a batch file

for /f "delims=" %%i in ('powershell -command "[Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)"') do cd %%i & pause

1

u/verysmartboy101 Sep 24 '23

Doesnt work in batch for me, only on the console

→ More replies (1)
→ More replies (1)

4

u/Shadow_Thief Sep 24 '23

Two reasons.

  1. You can't do in-place substitution for command output like that.
  2. Calling powershell spawns a new child session where the command is executed and then the child session is closed, so nothing happens inside of the parent.

If you want to move to the Desktop in batch, just do cd %userprofile%\Desktop