A Powershell Tutorial :Get Users Home Directory path from Active Directory with PowerShell:
We are currently doing some application migration at work and we are moving home folders from one server on one site to another. We have moved the user’s information to the new location. However some of the user’s home folder weren’t being mapped as it should as per the profile information for home folder. I needed to find out which server the user’s home folder are being directed to and being mapped to. There are about 140 users. I could go through each user account to see but it would be very time consuming.
Knowing that PowerShell has many powers, I decided to look into PowerShell to see if it can do that for me. Well, what do you know, I was able to do it with PowerShell. I will walk you through the code on how I did it.
Tasks:
- Get Users from the OU you need, or from the entire domain if that’s what you have
- Find out their home directory
- Get the current date
- Export the data into a csv file.
## Get-UserHomeDriectoryPath.ps1
## Powershell Tutorial
# August 6th 2014
# Johny Metellus
# This script will query AD to get information of the users and their home folder Path
# Make SearchBase is to search the OU (i.e.OU=xx,OU=xx,OU=DMI,DC=domain, DC=xx
###
# Get the current Date with the Get-Date command
$date = get-date -Format "dd-MMM-yyyy"
$SearchBase = “OU=OUName, DC=DomainNane, DC=”loc or net, com”
Get-ADUser -Filter * -SearchBase $SearchBase -Properties homedirectory |`
#Select only the fields you want to show in the csv file
Select-object GivenName, Surname, homedirectory |`
#Sort the data
Sort-Object homedirectory -Descending |`
# export the information into a csv file, make sure you specify the direction where to save the file and add the date that way you can have an accurate date and information
Export-csv "C:\AD\ad-hDrive-$date.csv"
#end of Script.
There it is, if you think of any way I can make this better, please let me know.
Leave a Reply