Avatar of amendala
amendala
 asked on

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

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.
Active DirectoryMicrosoft Server OSWindows Server 2008

Avatar of undefined
Last Comment
Neil Russell

8/22/2022 - Mon
Neil Russell

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

ASKER
To the best of my knowledge, there is no built-in Cmdlet named Search-ADAccount...
Neil Russell

http://technet.microsoft.com/en-us/library/ee617195
It is part of the Activedirectory Cmdlets
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Neil Russell

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 Russell

Ah my bad, you are 2008 Server not 2008 R2 ?
Jornak

The Active Directory Web Services must be installed/enabled in order to use the Active Directory Cmdlet(s) listed above.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
amendala

ASKER
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...
amendala

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

Hmm...
Neil Russell

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
amendala

ASKER
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 Russell

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
amendala

ASKER
First line fails.

"Search-ADAccount : Parameter set cannot be resolved using the specified named parameters."
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Neil Russell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question