1

When entering start shell:startup into the command prompt, it opens a new window at the startup folder. How can I print the path to that folder but not actually open the folder in explorer?

phuclv
  • 32,499
  • 12
  • 130
  • 417
Andoo
  • 734
  • 6
  • 22

1 Answers1

1

You can use .NET's Environment.SpecialFolder. This PowerShell command will give you the path of startup folder

[Environment]::GetFolderPath([Environment+SpecialFolder]::Startup)

If you really want to use cmd then call it like this

powershell -Com "[Environment]::GetFolderPath([Environment+SpecialFolder]::Startup)"

and use use for /f to save that to a variable like normal


It's possible to get the path using pure batch without resorting to PowerShell but much more trickier

There are 2 keys in the registry containing path to special folders: HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders and HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders. Here's the way to parse them taken from this answer:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "StartupFolder="
for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Startup 2^>nul') do if /I "%%I" == "Startup" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "StartupFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "StartupFolder=%%~K"
if not defined StartupFolder for /F "skip=1 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Startup 2^>nul') do if /I "%%I" == "Startup" if not "%%~K" == "" if "%%J" == "REG_SZ" (set "StartupFolder=%%~K") else if "%%J" == "REG_EXPAND_SZ" call set "StartupFolder=%%~K"
if not defined StartupFolder set "StartupFolder=\"
if "%StartupFolder:~-1%" == "\" set "StartupFolder=%StartupFolder:~0,-1%"
if not defined StartupFolder set "StartupFolder=%UserProfile%\Startup"

echo Startup folder is: "%StartupFolder%"

endlocal
phuclv
  • 32,499
  • 12
  • 130
  • 417