Creating a user inputted array for user profiles.

Hi All,

I'm currently trying to create a read-host array that will take in user names typed in and exclude them from a command that will be ran within an OU. Here is what I have so far:


#Start of script...

Import-Module ActiveDirectory


#Questions begin to determine variable data...


#Variable that represents the current graduated school...

$oldSchool = 'Test2'

$newSchool = 'Test3'

$ADgroup = 'CHS Students'

$ADgroupWeb = 'CHS Students Web'



#This loop goes in and creates an array of students to be entered who are excluded from the process for whatever reason...

$exclude = @()

do {

 $input = (Read-Host 'Are there any students that should not be moved?(Ex. Not graduating)

Please type in there username. To exit entering usernames hit enter leaving it blank or type quit or exit')

 if ($input -ne '') {$exclude += $input}


until ($input -eq '' -or 'quit' -or 'exit')



#TODO: Look into how objects are moved if the operation was stopped our halted 

#Here is where the entire folder graduation year container is moved to its new location using this command...

Move-ADObject 'OU=$oldSchool,OU=District,DC=csd,DC=local -exclude $exclude' -TargetPath 'OU=$newSchool,OU=District,DC=csd,DC=local'


#This begins with a three step process to remove the members from their groups...

# 1. Members are searched, found, and determined by these two commands and are set to a variable... 

$searchOU = 'OU=$newSchool,OU=District,DC=csd,DC=local'

$users = Get-ADuser -filter * -searchbase $searchOU -Properties DistinguishedName | Select -expandProperty DistinguishedName | -Properties memberOf


foreach ($user in $users) {

 Remove-QADMemberOf -Identity $user.dn -RemoveAll




#Add the users to their proper groups after being removed from their previous ones...

#Add-ADGroupMember -Identity $ADgroup $_ -Members $users -Confirm:$false  

#Add-ADGroupMember -Identity $ADgroupWeb $_ -Members $users -Confirm:$false


Some of it is commented out from testing piece by piece. But the point of this script is for a user to type in the school they want to take students from who are graduating and move them to the other school. They also will then type in usernames of students who aren't graduating and it will exlcude them from the move. I keep getting flagged an error saying:

+ Move-ADObject 'OU=$oldSchool,OU=District,DC=csd,DC=local -exclude $exclude' -Tar ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (OU=$oldSchool,O...xclude $exclude:ADObject) [Move-ADObject], ADIdentityNotFoundException

    + FullyQualifiedErrorId : Cannot find an object with identity: 'OU=$oldSchool,OU=District,DC=csd,DC=local -exclude $exclude' under: 'DC=csd,DC=lo 



-Properties : The term '-Properties' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the 

name, or if a path was included, verify that the path is correct and try again.

At C:\Users\singhj\Documents\GitHub\PowerShell\move\test.ps1:30 char:128

+ ... nguishedName | -Properties memberOf

+                    ~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (-Properties:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException


Not sure what it means. Is this the best method I'm doing to do a read-host array or is there a better way to do it? Let me know your thoughts. Thanks!

  • This line is the first problem:

    $searchOU = 'OU=$oldSchool,OU=District,DC=csd,DC=local'

    If you use single quotes around a string, then no evaluation will be done for any variables within. Therefore, your $oldSchool will never be replaced with 'Test2'.

    Use double-quotes for any strings where you need to evaluate the variables within.


    This line has the same problem:

    Move-ADObject -Identity $user -exclude $exclude -TargetPath 'OU=$newSchool,OU=District,DC=csd,DC=local'

Reply Children
No Data