Jump to content

Whoa, I found something that's actually better in Win 10


NoelC

Recommended Posts

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!!!!   :crazy:

-Noel

Link to comment
Share on other sites


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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...