modify batch script which outputs dsquery user results

Hey Experts!  It's been awhile since I posted a question on here and would appreciate any assistance you can offer in regards to this question.  Our office is under-going an extensive reorg and users are being moved to different departments.  Because of this, my memory of knowing which users are in which OU is no longer accurate.  I came up with a quick batch script to find the users but I want to clean-up the export to include only a couple pieces of information.

First, the script:
@echo off
cls

set findstring=
set /p "findstring=Enter in user's first name or string: "
DSQUERY USER ou=blah -name *"%findstring%"*

Open in new window


That outputs this at the command prompt:
"CN=x\, y ,OU=1,OU=2,OU=3,OU=4,DC=5,DC=6"

I only want the CN (which is the user name) and the ou that relates to OU=2 as the other information is irrelevant to what I need to gather.  What would I use to output only those two values?

Thanks again for your help!
LVL 9
samiam41Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Something like this?
@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER ou=blah -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0
samiam41Author Commented:
Hey oBdA!  Great to work with you again.

I took your code and replaced "ou=blah" with the full AD context.  I get this error:

Enter in user's first name or string: beth
dsquery failed:'depts' is an unknown parameter.

Depts is the first part of the AD context.

ou=depts,ou=x,dc=y,dc=z
0
samiam41Author Commented:
So this is what I'm using.

@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER ou=depts,ou=x,dc=y,dc=z, -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

oBdACommented:
Sorry, too much copy and paste going on. The OU path needs to be enclosed in double quotes:
@echo off
cls
set findstring=
set /p "findstring=Enter in user's first name or string: "
for /f "tokens=2,6 delims=,=" %%a in ('dsquery.exe USER "ou=depts,ou=x,dc=y,dc=z" -name *"%findstring%"*') do echo %%a [%%b]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
samiam41Author Commented:
Amazing work as always!  Thanks for the rapid responses
0
samiam41Author Commented:
Oops.  I may have closed it out a bit too quick.  I posted a new question so that you could help (if you have some time) and can earn a few more points in the process.

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_28667339.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.