Helper scripts
The command line processor supports some environment variables for system and user paths, or that can otherwise help building them. They are:
ALLUSERSPROFILE
APPDATA
CommonProgramFiles
CommonProgramFiles(x86)
CommonProgramW6432
COMPUTERNAME
HOMEDRIVE
HOMEPATH
LOCALAPPDATA
LOGONSERVER
ProgramData
ProgramFiles
ProgramFiles(x86)
ProgramW6432
PUBLIC
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERDOMAIN_ROAMINGPROFILE
USERNAME
USERPROFILE
windir
You can type set | more
to see the complete list along with the assigned values. There's no native way however to get particular paths which aren't listed, unless you can combine one or more variable and build the actual path manually.
Below you can find a couple of hybrid batch scripts which can retrieve the path associated to a specific shell folder. Save either code as ShellHelper.cmd
(or whatever you like, just keep the .cmd
extension). The scripts accepts a single parameter, which is the shell folder identifier (e.g. Favorites
). The path will be then stored in the %shellFolder%
variable.
Example usage
ShellHelper.cmd Favorites >nul
if defined shellFolder pushd "%shellFolder%"
Remarks
Unlike the Powershell version, the VBScript one works out of the box in Windows 2000 and later. As for your specific use, either version will do.
PowerShell is built-in in Windows 7 and later, but has to be manually installed in earlier operating systems.
VBScript version
Main credit goes to jeb and dbenham who come up with (and refined) the hybrid technique used here.
REM^ &@echo off
REM^ &set shellFolder=
REM^ &if "%~1" == "" exit /b 2
REM^ &for /f "delims=" %%G in ('"cscript /nologo /e:vbscript "%~f0" %~1 "') do set shellFolder=%%~G
REM^ &exit /b
WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders.Item(WScript.Arguments(0))
Supported identifiers
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Recent
SendTo
StartMenu
Startup
Templates
Note Additional shell folders can be supported by replacing the last line with:
WScript.Echo WScript.CreateObject("Shell.Application").Namespace(CLng(WScript.Arguments(0))).Self.Path
In this case, to get a particular shell folder path you need to pass its assigned numeric value. For example, to get the Favorites
path the value you have to use is 6
. You can find all values and their meaning in the linked article below.
Further reading
PowerShell version
@echo off
set shellFolder=
if "%~1" == "" exit /b 2
set _params=-NoLogo -NoProfile -Noninteractive -ExecutionPolicy Bypass
set _command="([Environment]::GetFolderPath('%~1'))"
for /f "usebackq delims=" %%G in (`powershell %_params% -Command %_command%`) do set shellFolder=%%~G
set _params=
set _command=
exit /b
Supported identifies
ApplicationData
CommonApplicationData
CommonProgramFiles
Cookies
Desktop
DesktopDirectory
Favorites
History
InternetCache
LocalApplicationData
MyComputer
MyDocuments
MyMusic
MyPictures
Personal
ProgramFiles
Programs
Recent
SendTo
StartMenu
Startup
System
Templates
PowerShell 3.0 / .NET Framework 4.0 and later
In addition to the identifiers above, these are available too:
AdminTools
CDBurning
CommonAdminTools
CommonDesktopDirectory
CommonDocuments
CommonMusic
CommonOemLinks
CommonPictures
CommonProgramFilesX86
CommonPrograms
CommonStartMenu
CommonStartup
CommonTemplates
CommonVideos
Fonts
LocalizedResources
MyVideos
NetworkShortcuts
PrinterShortcuts
ProgramFilesX86
Resources
SystemX86
UserProfile
Windows
Note To get the list of the available ones, you can run the following command in a PowerShell console:
[Enum]::GetNames('System.Environment+SpecialFolder') | Sort-Object
Further reading