[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to find all AD users with no Home Directory and set

Posted on 2013-12-05
5
Medium Priority
?
1,485 Views
Last Modified: 2013-12-05
Hi All

I have attempted to write a script to find all enabled users who have a null value for Home directory and then set one as we have a few hundred users who don't have one.

However I think my have a go mentality is greater than my powershell ability :)

$usersHD = Get-ADUser -Filter {(Enabled -eq "True")} -Properties * | Where-Object { $_.homedirectory -eq $null} 
 foreach ($user in $usersHD) 
{
set-aduser -homedrive u: -homedirectory - \\server\share\%username% 
}

Open in new window


I know line one works as if i look at the variable it lists all the users that do not have a Homedirectory, its the actual setting it that I cant get working

Can anyone help me out and explain were I am going wrong

Thanks
0
Comment
Question by:ncomper
  • 4
5 Comments
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 39698023
It's the %username% part that doesn't work for you (also you weren't directing the set-aduser command at any identity so you should have gotten an error).  If I recall correctly, setting this through the GUI does a dynamic substitution.  Pretty sure you would have to set this explicitly for each user.
 
foreach ($user in $usersHD) 
{
set-aduser $user.samaccountname -homedrive u: -homedirectory "\\server\share\$($user.samaccountname)" 
}

Open in new window

0
 
LVL 5

Author Comment

by:ncomper
ID: 39698054
That worked perfect thanks
0
 
LVL 5

Author Comment

by:ncomper
ID: 39698132
I've requested that this question be closed as follows:

Accepted answer: 0 points for ncomper's comment #a39698054

for the following reason:

Excellent worked perfect Thanks
0
 
LVL 5

Author Comment

by:ncomper
ID: 39698131
Didn't mean to  to accept my own comment
0
 
LVL 5

Author Closing Comment

by:ncomper
ID: 39698133
Excellent, worked perfect Thanks
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

873 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