Ying Li at myITforum.com

PowerShell & System Center

There is always a better way: PowerShell export-csv cmdlet

In my previous script PowerShell script to query ADSI and get user object properties

I created excel object and using PowerShell/ADSI to interact with AD and send the output to excel.

At the time of that witting, I was proud of what I did – otherwise, I wouldn’t publish it!

But not anymore!

I could get the similar results using the below script – one liner!

Get-QADUser -SizeLimit 0  -ip sAMAccountName, homedirectory, homedrive, Mail | Select name, sAMAccountName, HomeDirectory, HomeDrive, TsProfilePath, Mail |export-csv ADUsers.csv

Of course, in order to run the above script you need to download and install ActiveRoles Management Shell for Active Directory from Quest.

Let me explain the above script

Get-QADUser which will retrieve all users in a domain or container that meet the specified conditions. I heard Microsoft own version of PowerShell extension is in the work and it will be something like Get-ADUser. Quest was gracious enough to leave that cmdlet to Microsoft!

-Sizelimit 0 means return all – the default will return 1000.

You have to include -IncludeProperties (-ip) otherwise those properties won’t show. Thanks my fellow MVP friend Demitry Sotnikov, the author of Quest PowerShell extension for the tip.

Last but not list export-csv cmdlet will export the results to an csv file which as you know is excel’s poor cousin. Even though it is lacking some of the format such as Bold, autofit and colors etc. But I will still trade this one liner with my one page script!

There is always a better way…

Posted: May 13 2008, 07:55 PM by yli628 | with no comments
Filed under:

Comments

No Comments