Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2009-04-09
2
Medium Priority
?
3,276 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 71

Accepted Solution

by:
Chris Dent earned 500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

971 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