Solved

I need a simple powershell script to change home folder paths in AD

Posted on 2009-04-09
2
3,185 Views
Last Modified: 2013-12-24
Being new to PowerShell scripting, I'm having trouble setting up a script to change a select group of users and then change their home folder path in AD.  I need to base this on which OU they're in, so I can do a select by OU to get the users, then a "foreach" loop to change their homefolder path.  I selected a sample script and its printing out the group I want just fine, but when I get to the for each loop and try to write-host the home directory, then I fail miserably.  Yes I have the AD tools loaded.  Heres some of the script thats working. After "get-QADUser"  line, things go south quickly.
#Powershell script to change home folder
add-PSSnapin quest.activeroles.admanagement
#get-command Get-QADUser
$OU = "lwsd.k12wa.org/staff/Resource Center/MIS Dept/Net Test"
get-QADUser -SearchRoot $OU | format-Table FirstName, LastName, DisplayName, HomeDirectory -auto
users$ = get-QADUser -IncludeAllProperties -SearchRoot $OU
write-host users$
foreach ($user in $u)
{
write-host $user.HomeDirectory
}

Open in new window

0
Comment
Question by:LWSDTECH
2 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 125 total points
ID: 24136588

This should return all users with a Home Directory set in $OU:

Get-QADUser -SearchRoot $OU -HomeDirectory *

You can make the filter more specific, say you wanted only those on a specific server:

Get-QADUser -SearchRoot $OU -HomeDirectory "*server*"

If you want to see the HomeDirectory in the output you should be able to add any of these:

Get-QADUser -SearchRoot $OU -HomeDirectory "*server*" | Select-Object *
Get-QADUser -SearchRoot $OU -HomeDirectory "*server*" | Select-Object DN, HomeDrive, HomeDirectory

Then if you wanted to change  the value for each of the users in the results:

Get-QADUser -SearchRoot $OU -HomeDirectory "*server*" | %{ Set-QADUser -HomeDirectory "NewValue" }

% is an alias for the For-EachObject cmdlet, allowing you to loop through each of the returned results. NewValue doesn't have to be hard-coded like this either, we could replace the server name only (for example) with:

Get-QADUser -SearchRoot $OU -HomeDirectory "*server*" | %{ Set-QADUser -HomeDirectory $($_.HomeDirectory -Replace "server1", "server2") }

You do have to be a bit careful there, -Replace uses a Regular Expression to match, so you'll get some odd results should you include "\\server1" rather than just "server1".

Chris
0
 

Author Closing Comment

by:LWSDTECH
ID: 31568742
Ok, after a year of dealing with this stuff and not reviewing it like I should have, this would work fine.  I'm accepting it.  Thank you Chris.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question