Search code examples
windowscommand-promptspecial-folders

Echo directory opened by shell:startup


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?


Solution

  • 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 you can call the above command like this

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

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

    Alternatively use a hybrid Batch/VBS solution

    <!-- : Begin batch script
    @echo off
    cscript //nologo "%~f0?.wsf" %1
    exit /b
    
    ----- Begin wsf script --->
    <job><script language="VBScript">
    
    Set oShell = CreateObject("Wscript.Shell")
    Set oSFolders = oShell.SpecialFolders
    WScript.Echo oSFolders("Startup")
    
    </script></job>
    

    or hybrid Batch/Js

    @if (@CodeSection == @Batch) @then
    @echo off
    cscript //e:jscript //nologo "%~f0" %*
    exit /b
    @end
    
    // JScript Section
    WScript.Echo((new ActiveXObject("shell.application")).namespace(0x07).Self.Path);
    

    0x07 here is the Shell Special Folder Constant for startup

    Just save the above 2 snippets as a *.bat file and run. Or strip the batch part and make it a pure VBS/Js solution.

    In fact these snippets use COM objects so they can be written in any languages that support COM objects, for example in PowerShell:

    (New-Object –ComObject Shell.Application).namespace(0x07).Self.Path
    

    or

    $c = New-Object -ComObject Wscript.Shell
    $c.SpecialFolders("Startup")
    

    It's possible to get the path using pure batch without resorting to PowerShell, VBS or Js 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