Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-09
2
Medium Priority
?
3,255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

688 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