Need Help with Advanced Function Using Input GUI and Active Directory Cmdlets

Attached is a fairly simple script that contains two functions. One function is a Read-InpuGUI function that I found online and the other is my Check-ADUserExist advanced function.

The script works fine for one user, but when I run the following command to call on the Read-InputGUI function to display the multi line input box to enter multiple users, and then click OK, the script will NOT produce any results - period! If I enter only one user in the multi line GUI, it will produce output just fine.

It only has issues with multiple users.

If I output the data contained in the $UserList variable to a text file to hold all the user names and then use Get-Content to grab that data, then all the users will get processed. I don’t want to have to do it this way though, since I think producing text files like this is kind of sloppy when I know there is a better way of doing it.

Command to use the input GUI:

Check-ADUserExist -ADUserName “userlist”

To check if my $UserList variable was holding multiple users, I simply output the data to the screen and all the users were displayed with no issue.

The problem looks to be that the Active Directory cmdlet may an an issue processing multiple users in this fashion using an input GUI, but that is just speculation.

Here is where it appears the issue is:

ElseIf ($ADUserName -eq "UserList")
			$UserList = Read-InputGUI "Please Enter User Names Here:" "User List" "Input user names here and hit the Enter button for each user name entry"
			Foreach ($ADDomain in $ADDomains)
				Foreach ($ADUser in $UserList)
					$ADUserQuery = (Get-ADUser -Filter {sAMAccountName -eq $ADUser} -Server $ADDomain).sAMAccountName
					If ($ADUserQuery -eq $ADUser)
						Write-Output "$ADUser exist in $ADDomain"

I would greatly appreciate an explanation why this cannot get results for multiple users and\or some help to resolve this.

Thanks everyone


Looking at your attachment you have this line

$QueryADUserName = (Get-ADUser -Filter {sAMAccountName -eq $ADUserName} -Server $ADDomain).AMAccountName

AMAccountName should be samAccountName

I’d put some write-verbose statements into your code to test which domain you are in, that your call to Get-ADuser has actually worked, that the correct values are coming though as $ADuser. Also check that the data you are getting from $userlist doesn’t contain trailing spaces as that will impact your results

Read-InputGUI is returning a single String value (which happens to contain multiple lines). PowerShell won’t automatically enumerate those lines with a foreach loop; you need to split the string into an array first. It’s probably also a good idea to filter out any blank / whitespace lines, in case the user entered something weird. Try this:

Foreach ($ADUser in $UserList -split '\r?\n' -match '\S')



It worked dude!

Thanks again for your help brother.

Richard, yeah that was a typo bro, sorry about that.