April 17, 201610 yr In Windows 8.1... If you run a scheduled task as a particular user, and if that user is not logged on at the time, when you use the %USERPROFILE% environment variable, in Win 8.1 you get the default user profile path. In other words, where I would have expected USERPROFILE to be C:\Users\NoelC, instead it's delivered to the scheduled task as C:\Users\Default. USERNAME is set as expected (NoelC) though. So if one wants to use the user profile area, one has to code the command: SET USERPROFILE=C:\Users\%USERNAME% In Windows 10... When I tested it, I actually got C:\Users\NoelC in the USERPROFILE variable. OMG, I'm going to have to upgrade all my machines immediately to eliminate this horrendous, critical bug!!!! -Noel
April 17, 201610 yr I imagine that this wouldn't affect your situation, but in general I assume it would be better in Windows 8.1 to save the current value of USERPROFILE first, and then restore it after you were done? Just thinking of situations where more than one user would be involved? I don't know for sure since it originally had the value in your situation of C:\Users\Default, but just playing devil's advocate. Cheers and Regards
April 17, 201610 yr Author That's a reasonable thought, but the SET command is only in force for the duration of the job, which is a "go gather some system data, log it, and exit", so the change isn't permanent. Were you thinking the SET command would affect things system-wide? -Noel
April 17, 201610 yr I knew the duration limit was true for "new" variables, but wasn't sure when changing a built-in system variable like USERPROFILE. I've also only had to mess with a single user system. I guess I was thinking about the ability to change the value of things like PATH, which would stick after a job was done, but that might be a special case? No, I'm wrong. Looking at http://ss64.com/nt/path.html, I see that even changes to PATH via SET are only valid for the session. Permanent changes require use of the the control panel or AUTOEXEC.BAT. Oh well, my mistake. Never mind. Cheers and Regards
April 17, 201610 yr Or of SETX (or similar), JFYI:http://ss64.com/nt/setx.html http://www.codeproject.com/Articles/12153/SetEnv Specifically for PATH, PATHED should do: http://superuser.com/questions/268287/adding-path-with-setx-or-pathman-or-something-elsehttp://www.p-nand-q.com/download/gtools/pathed.html jaclaz
April 18, 201610 yr 22 hours ago, NoelC said: In Windows 8.1... If you run a scheduled task as a particular user, and if that user is not logged on at the time, when you use the %USERPROFILE% environment variable, in Win 8.1 you get the default user profile path. In other words, where I would have expected USERPROFILE to be C:\Users\NoelC, instead it's delivered to the scheduled task as C:\Users\Default. USERNAME is set as expected (NoelC) though. So if one wants to use the user profile area, one has to code the command: SET USERPROFILE=C:\Users\%USERNAME% Just in case there's been some sort of redirection as opposed to symbolic linking it may be worth not hardcoding the drive letter or even the \Users locations. Off the top if my head, (untested), something like this may do that: @For /F "Tokens=2 Delims=," %%a In ('WhoAmI /User /Fo CSV /NH') Do @( For /F "Tokens=1* Delims==" %%b In ( '"WMIc Path Win32_UserProfile Where (SID='%%~a') Get LocalPath /Value"' ) Do @For %%d In (%%c) Do @Set UserProfile=%%d)
Create an account or sign in to comment