PowerShell logo

Powershell – Get-ADuser and output the homedrives to CSV file

I had a customer with around 27 file servers used as locations for AD home drives. We needed to do some analysis on which users used which server, as things like DFS or a strategy of where to place users were not in place. So PowerShell to the rescue.

A simple version of this script is;

get-aduser -Filter * -properties * | select DisplayName,Enabled,HomeDirectory,LastLogonDate,CanonicalName | Export-csv -path c:\scripts\userhomefolder.csv

I created this more complex script after the amount of unique objects exceeded the maximum filter within excel, so by splitting into a file per server fixed this.

First off, create an array with the multiple file servers, then used the “foreach” command to loop a PowerShell command with each file server name.

We look into all user’s in AD and output to a CSV file any users with file server X into a CSV file.

#Add the AD module into the Powershell session
Import-module ActiveDirectory

#Array containing each File Server, can be FQDN or short name
$fileservers = 'FS1','FS2','FS3'

#Loop to run a script for each object in the array against all AD users, outputs in CSV to C:\ folder
Foreach ($fileserver in $Fileservers)
{
get-aduser -Filter * -properties * | select DisplayName,Enabled,HomeDirectory,LastLogonDate,CanonicalName | Where {$_.HomeDirectory -like "*$fileserver*"} |Export-csv -path c:\scripts\userhomefolder2-$fileserver.csv
}

 

Regards

 

Dean

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.