2

I am trying to create a batch file which installs favorites on all company PCs and Notebooks.

Is it possible to change the directory in CMD using shell:favorites?

I need this because on the Notebooks the favorites are stored locally on C:\%userprofile%\Favorites and on the Desktop the %userprofile% in general is also on the C:\ drive but the favorites are saved on the home drive. We don't use letters like H:\ to map the drive, just a network shortcut, this is why I'm trying so hard to get this working.

We have different servers in different countries, therefore I don't want to adjust my batch script for every country with the according network path to the local server.

2 Answers 2

1

It can be done using powershell

cd ([Environment]::GetFolderPath("Favorites"))
3
  • Thanks for the tip! so I guess it's not possible with CMD only?
    – Martin
    Mar 24, 2014 at 11:54
  • Can I use this to launch from cmd with powershell - command?
    – Martin
    Mar 24, 2014 at 12:38
  • You can run it from cmd, but current directory will be changed only for powershell session, not parent cmd. But you can run powershell -c "[Environment]::GetFolderPath(\"Favorites\")" and pipe its output somehow
    – Andrey
    Mar 24, 2014 at 16:33
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .