Get 'proper' user profile paths

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Get 'proper' user profile paths

#1 Post by Compo » 26 Jan 2016 06:27

I saw a question in a newsgroup a couple of days ago and noted that at least a couple of the members from this Forum had provided some input.

As I do not wish to use Google Groups and no longer have an installed newsreader I thought I'd make my suggestion here and leave open for comment/improvement.

At least one of the suggestions within the responses given has used WMIC, as does mine.

From Console:

Code: Select all

For /F "UseBackQ Tokens=1* Delims==" %A In (`WMIc Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`) Do @For %C In (%B) Do @Echo=%C

From Script:

Code: Select all

@For /F "UseBackQ Tokens=1* Delims==" %%A In (
   `WMIc Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`
   ) Do @For %%C In (%%B) Do @Echo=%%C&Pause>Nul

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get 'proper' user profile paths

#2 Post by foxidrive » 26 Jan 2016 08:59

Is this designed to return the current user profile, compo? It only returns my own user profile here.
Oops. I guess that may be because there is only one user profile on this PC. :D

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Get 'proper' user profile paths

#3 Post by Compo » 26 Jan 2016 13:02

foxi, as you probably already know, it should provide all the 'proper' users profile paths, filtering out those weird hidden/system accounts we don't use.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get 'proper' user profile paths

#4 Post by foxidrive » 27 Jan 2016 06:18

Compo wrote:it should provide all the 'proper' users profile paths


Yes, ta Compo.

There's a simple way of getting the user accounts - it does includes the public account but a findstr filter can get rid of that.

Code: Select all

dir "%USERPROFILE%\.." /b


Code: Select all

dir "%USERPROFILE%\.." /b|findstr /beiv "public"

Matt Williamson
Posts: 82
Joined: 30 Dec 2013 10:16
Location: United States by the big waterfall

Re: Get 'proper' user profile paths

#5 Post by Matt Williamson » 27 Jan 2016 08:38

Here is another way of framing your WMIC query. It will remove the need to use backquotes and if you ever have to use ^'s to escape, it will take care of them too. I just think it's easier to read too.

Code: Select all

@for /f "Tokens=1* Delims==" %%A in (
  '"WMIC Path Win32_UserProfile Where (Special!='True') Get LocalPath /Value"'
   ) Do @For %%C In (%%B) Do @Echo=%%C&Pause>Nul
exit /b

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Get 'proper' user profile paths

#6 Post by Compo » 27 Jan 2016 12:07

foxidrive wrote:There's a simple way of getting the user accounts - it does includes the public account but a findstr filter can get rid of that.

Code: Select all

dir "%USERPROFILE%\.." /b


Code: Select all

dir "%USERPROFILE%\.." /b|findstr /beiv "public"
I can't say for sure, because I'm not currently in the vicinity of one, but I think if you redirect a profile folder, (more common than you think), the location of the parent directory for %UserProfile% isn't necessarily the same as the parent directory location for any of the other users.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Get 'proper' user profile paths

#7 Post by Ed Dyreen » 27 Jan 2016 13:27

Too bad Path Win32_UserProfile is an unknown wmi class on my xp pro sp3 :(

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Get 'proper' user profile paths

#8 Post by Compo » 27 Jan 2016 14:36

Ed Dyreen wrote:Too bad Path Win32_UserProfile is an unknown wmi class on my xp pro sp3 :(
The class may not have been implemented until Vista SP1, (not that I would be creating new solutions for Operating Systems that far out of date either).

One last alternative, just in case it's there but not found by default is to use the namespace location.

Code: Select all

For /F "UseBackQ Tokens=1* Delims==" %A In (`WMIc /NameSpace:\\Root\CIMv2 Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`) Do @For %C In (%B) Do @Echo=%C

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Get 'proper' user profile paths

#9 Post by Ed Dyreen » 27 Jan 2016 16:22

Compo wrote:One last alternative, just in case it's there but not found by default is to use the namespace location.

Code: Select all

For /F "UseBackQ Tokens=1* Delims==" %A In (`WMIc /NameSpace:\\Root\CIMv2 Path Win32_UserProfile Where "Special!='True'" Get LocalPath /Value`) Do @For %C In (%B) Do @Echo=%C
I used scriptomatic to figure out it is not there.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get 'proper' user profile paths

#10 Post by foxidrive » 28 Jan 2016 22:12

Compo wrote:I can't say for sure, because I'm not currently in the vicinity of one, but I think if you redirect a profile folder, (more common than you think), the location of the parent directory for %UserProfile% isn't necessarily the same as the parent directory location for any of the other users.


You mean to use a non-standard location for the c:\users folder?

I am quite certain that Windows will use the non-standard location correctly, because it's in the registry and that's where Windows gets the %UserProfile% details from.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Get 'proper' user profile paths

#11 Post by Compo » 29 Jan 2016 09:41

Basically unless all users are redirected using the same location, (e.g. REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /V ProfilesDirectory), they wont all show using %UserProfile%\..

Example:
1 PC, 3 HDDs - HDD1 (OS/System Drive 160GB) C:, HDD2 (Childrens Data Drive 500GB) D:, HDD3 (Parents Data Drive 2TB) E:

Son redirects %UserProfile% directory to D:\Kids\Son
Daughter redirects %UserProfile% directory to D:\Kids\Daughter
Father redirects %UserProfile% directory to E:\Profiles\Father
Mother redirects %UserProfile% directory to E:\Profiles\Mother

%UserProfile%\.. will only identify two users.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Get 'proper' user profile paths

#12 Post by foxidrive » 29 Jan 2016 14:28

Compo wrote:Basically unless all users are redirected using the same location, (e.g. REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" /V ProfilesDirectory), they wont all show using %UserProfile%\..

Example:
1 PC, 3 HDDs - HDD1 (OS/System Drive 160GB) C:, HDD2 (Childrens Data Drive 500GB) D:, HDD3 (Parents Data Drive 2TB) E:

Son redirects %UserProfile% directory to D:\Kids\Son
Daughter redirects %UserProfile% directory to D:\Kids\Daughter
Father redirects %UserProfile% directory to E:\Profiles\Father
Mother redirects %UserProfile% directory to E:\Profiles\Mother

%UserProfile%\.. will only identify two users.


Are you able to redirect different user folders to different locations?

Post Reply