Solved

Scripting help, moving users to OU powershell

Posted on 2014-11-03
7
97 Views
Last Modified: 2015-04-15
Hey guys, i have a command. This command will be the foundation of a script, and here is where i need help.
The command is

Get-ADUser -filter {(title -like "Teacher A") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'DC=Domain, DC=Local'

I hvae to do this for about 50 teachers. I could copy and manually type this command over and over to account for each individual teacher, but is there an easier way to achieve that?
0
Comment
Question by:mrbayIt
  • 4
  • 3
7 Comments
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40421086
Modify your filter for the Get-ADUser command so that it returns what you want.  When you use the -like operator you can use the wildcard "*".  So it all depends on if you can create something that will match all your desired accounts (and no others), and that depends on any similarity between accounts for a specific attribute.  Perhaps the following would work.
Get-ADUser -filter {(title -like "Teacher *") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'DC=Domain, DC=Local'

Open in new window

0
 

Author Comment

by:mrbayIt
ID: 40421814
sounds like a good start, but how do i identify the teachers for the wildcard to work? (i am new to scripting)
0
 
LVL 39

Expert Comment

by:footech
ID: 40422067
That's kind of up to you.  You know how the * wildcard works, right?
Is the title for all teachers similar?
If you're familiar with the use of wildcards at a command prompt, it's pretty much the same concept.  When you're dealing with files, you're only matching against the name.  So you have a command like
dir *.log
dir read*.txt

When dealing with AD objects, you have a lot more possible properties to match against if you so desire.  If we just focus on using the "title" attribute that's fine.  I can't tell you what you have in the title attribute for each user though.  You have to find a word (or part of a word) that's common across all the accounts you want.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:mrbayIt
ID: 40422131
Thanks footech, im new to scripting so no i dont fully understand wildcards.

more specifically, there is no common title, the job title field will have each teachers name inside of it. Here is what i mean

Get-ADUser -filter {(title -like "TeacherNameC") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameC,DC=Domain, DC=Local'
Get-ADUser -filter {(title -like "TeacherNAMEA") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameA,DC=Domain, DC=Local'
Get-ADUser -filter {(title -like "TeacherNameB") -and (company -eq "School Site A")} | Move-ADObject -TargetPath 'OU=TeacherNameB,DC=Domain, DC=Local'

is there an easier way to do this, instead of having to write out each teachers individual name mapping it to there own OU
0
 
LVL 39

Expert Comment

by:footech
ID: 40422198
A * means to match zero or more characters, no matter what they are.

What's the point of the title if all it contains is the teacher's name?  There's little more I can suggest here.  You need to find an attribute that each teacher has in common.  If there isn't one, well...  Accounts can be grouped together for different purposes, often by placing them within the same OU, or by making them members of security groups, etc.  If you need to manage accounts according to certain criteria, then you need to design your AD around that criteria.

I don't think I would ever support the idea of each account having its own OU.  I can't think of any logical reason to do so.
0
 

Author Comment

by:mrbayIt
ID: 40422242
Just haven't gone into detail, long story short i used TeacherNameA just for example, this rule really is meant to correspond to students. Thousands of students.

Every Student has there teacher listed under "Job Title" in AD. The script will need to place them in the teachers OU. Ive tested it already, which is why i referenced the original command its just that i am trying to automate this in a more efficient manner rather than writing each teachers name over and over.

Does that make sense now?
0
 
LVL 39

Expert Comment

by:footech
ID: 40422359
That makes a lot more sense.
To do what you're asking requires a list of all the teacher names.  Hopefully the names in the title attribute match exactly the name of the OU.  This list of names can either be obtained from a text file, hard-coded into the script, or perhaps pulled from existing OU structure.  Here's an example of what might work.
$names = "teacher1","teacher2"
foreach ($name in $names)
{
    Get-ADUser -Filter {(Title -eq $name) -and (company -eq "School Site A")} | Move-ADObject -TargetPath "OU=$name,DC=Domain,DC=Local" -WhatIf
}

Open in new window

0

Featured Post

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

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

18 Experts available now in Live!

Get 1:1 Help Now