We help IT Professionals succeed at work.

Query Active directory for user's profile path

Medium Priority
5,922 Views
Last Modified: 2012-05-06
I need to figure out a way to query active directory for a users profile path.  I need to be able to script this with command line input and output.  I want to be able to feed a script a username and the only thing it would output is that user's profile path (or ultimately just the profile server)I also do not want to have to enter the LDAP information.  

What I was trying to use was ADFind: adfind.exe -f "sAMAccountName=administrator"  
Which gives me a lot of information, but no Profile path.  

If anyone has something along those lines that will give me the profile information, I am all ears.  Thank you !
Comment
Watch Question

DrDave242Principal Support Engineer
CERTIFIED EXPERT

Commented:
The "dsget user" command used with the "-profile" switch will return a user's profile path.  It requires a user's distinguished name as input, but you can use the "dsquery user" command to get that DN, then pipe dsquery's output directly to dsget.  Here's more info on it, including a couple of examples:

http://technet.microsoft.com/en-us/library/cc732535.aspx

Have a look at "dsquery user" as well:

http://technet.microsoft.com/en-us/library/cc725702.aspx
CERTIFIED EXPERT
Top Expert 2013

Commented:
for adfind you can run
adfind -b dc=yourdomain,dc=com (or whateveryour suffix is)  -f "samaccountname=username" homedirectory
That can be expanded to search for all users
 
Thanks
Mike
What do you mean you do not want to have to enter the LDAP information?  You'll need to enter -some- qualifying information, such as the username, or any query tool isn't going to be very useful for you.

To restrict your query to only the Home Directory path, query against the 'homeDirectory' attribute - if nothing is returned for a particular user, it means that the attribute isn't populated.

To use adfind as in your example:

adfind -default -f "sAMAccountName=administrator" homeDirectory

PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:

ADFind does return the profile path (profilePath) attribute. You're referring to the "Profile Path" field as seen in AD Users and Computers?

Chris

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Save this as "ProfileQuery.cmd" or whatever.cmd; it expects the user's samid as argument and will only echo the profile path (if specified in the user's properties). It will leave with error level 1 if the profile path is empty.

@echo off
setlocal
set Profile=
for /f "tokens=1*" %%a in ('dsquery user -samid "%~1" ^| dsget user -profile -l ^| find /i "profile:"') do set Profile=%%b
if "%Profile%"=="" (
  echo Error: couldn't find user account, or no profile specified
  exit /b 1
) else (
  echo %Profile%
)

Open in new window

Author

Commented:
profilePath was actually what I was looking for.  If I use that with ADFind it gets me exactly what I need ! ( I wasn't able to see the profilepath from the information I had because sadly I had no profile set for the administrator account)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.