系统用户运行批处理时,如何访问指定用户的%APPDATA%(SCCM场景)
I get it—deploying apps with SCCM can be tricky when user-specific folders are involved, especially since SCCM runs as the SYSTEM account by default. Let's break down why your script is failing and how to fix it.
Root Cause
When SCCM runs your batch script as SYSTEM, the %APPDATA% environment variable points to the system profile (C:\Windows\System32\config\systemprofile\AppData\Roaming\), not the individual user's profile. That's why your preferences file ends up in the wrong place when using PSEXEC to simulate SYSTEM context.
Solution 1: Modify Your Batch Script to Target All User Profiles
This approach will install Wireshark, copy the preferences to the default user profile (so new users inherit it automatically), and push the file to all existing user profiles. Here's the updated script:
@echo off REM Install Wireshark silently Wireshark-win64-2.4.6.exe /S REM Copy preferences to Default User profile (for new users) mkdir "C:\Users\Default\AppData\Roaming\Wireshark" 2>nul xcopy /Y "preferences" "C:\Users\Default\AppData\Roaming\Wireshark\" REM Copy preferences to all existing user profiles (excluding system accounts) for /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /v ProfileImagePath ^| findstr /v "SystemProfile" ^| findstr /v "Public"') do ( set "userProfile=%%b" mkdir "%userProfile%\AppData\Roaming\Wireshark" 2>nul xcopy /Y "preferences" "%userProfile%\AppData\Roaming\Wireshark\" )
Key Details:
2>nulsuppresses error messages if the Wireshark folder already exists./Yinxcopyoverwrites existing preferences files without prompting.- The
reg querycommand pulls all user profile paths from the registry, then we filter out system accounts (SystemProfile and Public) to avoid unnecessary copies. - Make sure the
preferencesfile is in the same directory as your batch script (or use a full path to where it's stored in your SCCM package source).
How to Test:
Use PSEXEC to run the script as SYSTEM to verify it works:
psexec -s cmd.exe /c "path\to\your\script.bat"
Check the Default User folder and existing user profiles to confirm the preferences file was copied correctly.
Solution 2: Deploy Preferences via User Context (SCCM User Package)
If you prefer to handle user-specific settings separately from the machine installation, create a second SCCM package that runs in user context:
- Create a new package with just your
preferencesfile. - Create a batch script for this package:
mkdir %APPDATA%\Wireshark\ 2>nul xcopy /Y "\\your-sccm-server\package-source\preferences" "%APPDATA%\Wireshark\" - Deploy this package as a required user deployment (not machine) to all relevant users. This ensures the file is copied to the user's
%APPDATA%when they log in.
Why This Works:
When deployed in user context, %APPDATA% correctly points to the logged-in user's Roaming folder. This is great for ensuring even users who log in after the initial machine deployment get the preferences.
Recommendation
Combine both solutions for full coverage:
- Use the modified machine script to install Wireshark and set up preferences for existing users and new users via the Default profile.
- Deploy the user package as a fallback to catch any edge cases (like users who created profiles after the machine deployment).
内容的提问来源于stack exchange,提问作者Chris Higg

