How to Get the Public User's Profile Directory in Batch Script
In a multi-user Windows environment, you often need to place files or shortcuts in a location that is accessible to every user who logs into the machine. This is the purpose of the Public user profile. Common examples include placing a shortcut for a shared application on every user's desktop or providing a common set of documents for all users. Hardcoding a path like C:\Users\Public is unreliable, as the Users folder can be on a different drive or have a different name.
This guide will teach you the standard, built-in method for reliably finding the path to the Public user's profile directory using the %PUBLIC% environment variable.
The Core Method (Recommended): The %PUBLIC% Variable
The simplest, most direct, and most reliable method for finding the Public profile directory is to use the built-in %PUBLIC% environment variable. This variable is set by the system and always points to the correct location.
Syntax: "%PUBLIC%"
This variable expands to the full, absolute path of the Public user profile, which by default is C:\Users\Public.
The Alternative Method (Advanced): Querying the Registry
To understand where the %PUBLIC% variable gets its value, you can query the Windows Registry. This is the authoritative source for system-wide profile paths.
Syntax: REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v Public
This command queries the ProfileList key for the value named Public. This is useful for diagnostics but is more complex than simply using the environment variable in a script.
Basic Example: A Simple "Show Public Path" Script
This script displays the path provided by the %PUBLIC% variable and lists the contents of the Public Desktop.
@ECHO OFF
ECHO --- Finding the Public User Profile Path ---
ECHO.
ECHO The Public profile directory is located at:
ECHO %PUBLIC%
ECHO.
ECHO Listing the contents of the Public Desktop folder:
DIR "%PUBLIC%\Desktop"
Output:
--- Finding the Public User Profile Path ---
The Public profile directory is located at:
C:\Users\Public
Listing the contents of the Public Desktop folder:
Volume in drive C is Windows
...
Directory of C:\Users\Public\Desktop
...
How the %PUBLIC% Variable Works
The %PUBLIC% variable is a standard, system-level environment variable. Its value is set by the operating system based on the Public value in the registry key HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList. Because it's a system variable, it is available in all user sessions and provides a consistent and reliable way to find this shared folder.
Common Pitfalls and How to Solve Them
Problem: The Path Contains Spaces
While the default path C:\Users\Public does not contain spaces, it is possible on some systems for this path to be different. The universal rule in batch scripting is to always assume a path might contain spaces.
Solution: Always quote your paths. By enclosing the variable in double quotes (e.g., "%PUBLIC%\Desktop"), you ensure your script will work reliably, regardless of the path's contents.
Problem: Permissions for Writing to Public Folders
While the Public folder is generally readable by all users, writing to its subdirectories (especially Public Desktop or the public Start Menu) often requires administrative privileges.
Solution: Any script that needs to create files or shortcuts in these shared public locations must be run as an Administrator.
Practical Example: Creating a Shortcut on the Public Desktop
This is a very common administrative task. This script creates a shortcut for a shared application on the desktop of every user on the machine. This script must be run as an Administrator.
@ECHO OFF
SETLOCAL
TITLE Create Public Shortcut
SET "ShortcutName=My Shared App"
SET "TargetExe=C:\Program Files\MyCompany\SharedApp.exe"
SET "PublicDesktopPath=%PUBLIC%\Desktop"
SET "ShortcutFile=%PublicDesktopPath%\%ShortcutName%.lnk"
ECHO --- Creating a Shortcut for All Users ---
ECHO.
IF NOT EXIST "%TargetExe%" (ECHO [ERROR] Target executable not found. & GOTO :End)
ECHO Shortcut will be placed at: "%ShortcutFile%"
REM --- Use PowerShell to create the shortcut file ---
powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut('%ShortcutFile%'); $s.TargetPath = '%TargetExe%'; $s.Save()"
ECHO.
ECHO [SUCCESS] Shortcut has been created on the Public Desktop.
ECHO It will appear the next time users log in.
:End
ENDLOCAL
Conclusion
Using the Public user profile is the correct way to make files and shortcuts available to all users on a single computer.
Key takeaways:
- The
%PUBLIC%environment variable is the standard, simplest, and most reliable method for getting the path. - Common subfolders include
%PUBLIC%\Desktop,%PUBLIC%\Documents, and%PUBLIC%\Music. - Always enclose the path in double quotes (
"%PUBLIC%\...") to handle spaces correctly. - You must run your script as an Administrator to write files or create shortcuts in most of the Public profile subdirectories.