Solved

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

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to split this type of line? 5 84
Fixing Non-Standard characters in text 8 92
Save conflict messages on existing documents 15 55
Online Forms 6 53
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now