Solved

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

Posted on 2009-04-09
2
3,177 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Read about achieving the basic levels of HRIS security in the workplace.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now