We help IT Professionals succeed at work.

Need a PowerShell script to list the CNs of all users in a specific OU that are enabled.

1,191 Views
Last Modified: 2012-05-30
Folks -

I need some PowerShell code wipped up to list the CNs of all users within a specific OU that are ENABLED.  Simple, straightforward, but I'd like it without the use of the Quest cmdlets.  I realize this is easier with them but security restrictions require me to use the built-in PowerShell functionality.

The OU can be embedded int he code as a variable, it's easy enough to change.

Thanks in advance.
Comment
Watch Question

Neil RussellTechnical Development Lead

Commented:
Search-ADAccount -SearchBase "OU=YourOU,DC=yourdomain, DC=com" | Where{$_.enabled -eq $true} | %{Get-ADUser $_.ObjectGuid} | select name, givenname, surname | export-csv c:\users\username\desktop\unusedaccounts.csv -NoTypeInformation

Or some simular variation.

Author

Commented:
To the best of my knowledge, there is no built-in Cmdlet named Search-ADAccount...
Neil RussellTechnical Development Lead

Commented:
http://technet.microsoft.com/en-us/library/ee617195
It is part of the Activedirectory Cmdlets
Neil RussellTechnical Development Lead

Commented:
There are NO Built-in commandlets. A commandlet by its very nature is not builtin.
When you install eith RSAT or the AD Role on a server then the powershell commandlets for AD are installed.
Neil RussellTechnical Development Lead

Commented:
Ah my bad, you are 2008 Server not 2008 R2 ?

Commented:
The Active Directory Web Services must be installed/enabled in order to use the Active Directory Cmdlet(s) listed above.

Author

Commented:
No, for the most part you got it right... I'm on 2008 R2 and I was within the AD module, however, I mistyped Search-ADAccount.  That said, I'm still having an issue with the code complaining about being unable to resolve the name using the parameters specified.  I'm working that out.

Technically, and I'm not trying to push buttons, there are built-in commands, thousands of them.  :)  Get-Help being one...

Author

Commented:
What I get is "Parameter set cannot be resolved using the specified named parameters."

Hmm...
Neil RussellTechnical Development Lead

Commented:
So you have that whole statement on a single line?
Search-ADAccount -SearchBase "OU=YourOU,DC=yourdomain, DC=com" | Where{$_.enabled -eq $true} | %{Get-ADUser $_.ObjectGuid} | select name, givenname, surname | export-csv c:\users\username\desktop\unusedaccounts.csv -NoTypeInformation

Open in new window

Author

Commented:
Yup, still fails.

I've tried dropping it all the way back to just "Search-ADAccount -SearchBase "OU=MyOU,DC=MyDomain,DC=lcl" and it fails there.  The DN I'm using is valid and I've tried running the AD module with and without elevated privs.
Neil RussellTechnical Development Lead

Commented:
Ok break it down and see whats failing for you...

$Temp = Search-ADAccount -SearchBase "OU=YourOU,DC=yourdomain, DC=com" | Where{$_.enabled -eq $true}

$Temp2 = $Temp | %{Get-ADUser $_.ObjectGuid} | select name, givenname, surname

$Temp2 | export-csv c:\users\username\desktop\unusedaccounts.csv -NoTypeInformation

Author

Commented:
First line fails.

"Search-ADAccount : Parameter set cannot be resolved using the specified named parameters."
Technical Development Lead
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*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.