Get-ADUser : Cannot Validate argument 'identity'. The argument is null - powershell

I'm working on a script and part of it relies on selecting a AD user from a list box. Problem is that the selected user is coming back 'Null'. Have a look at the code below!
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Account Selection'
$form.Size = New-Object System.Drawing.Size (400,250)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point (110,165)
$OKButton.Size = New-Object System.Drawing.Size (75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point (190,165)
$CancelButton.Size = New-Object System.Drawing.Size (75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point (10,0)
$label.Size = New-Object System.Drawing.Size (280,20)
$label.Text = 'Select the user account'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point (10,40)
$listBox.Size = New-Object System.Drawing.Size (363,150)
$listBox.Height = 120
$form.Controls.Add($listBox)
$form.Topmost = $true
$ADUserGroup = Get-ADObject -Filter 'ObjectClass -eq "User"' -SearchBase 'OU=Users,DC=Company,DC=com' | sort name
foreach ($User in $ADUserGroup)
{
$listBox.Items.Add($User.Name) | Out-Null
}
$result = $form.ShowDialog()
#Store results
if ($result -eq 'Cancel') {exit}
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$Name = $listBox.SelectedItem
$Employee = Get-ADUser -Filter {SamAccountName -eq $Name}
}
Get-ADUser -Identity $Employee
After the user is selected we should be able to run more AD related commands using the $Employee variable. Below is the error.
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:69 char:22
+ Get-ADUser -Identity $Employee
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException

It must be that your $Employee variable is null when it gets to the Get-ADUser -Identity $Employee call. $Employee comes from the line $Employee = Get-ADUser -Filter {SamAccountName -eq $Name}, so it must be that AD can't find a user with SamAccountName = $Name.
Write some output for the $Employee variable and see if it is indeed null. Then figure out whether the $Name variable is correct and if that person exists in AD.

I would suggest using the listboxes SelectedIndex instead of the SelectedItem property.
Also, instead of using Get-ADObject, why not use Get-ADUser in the first place.
Taken from the part where the form is built, just below $form.Topmost = $true, this should work for you:
# Get an array of all user objects in the given OU and sort by property Name
# By default, these objects will have the following properties:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass,
# ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
# If you need more or other properties, then you need to add the -Properties
# parameter to the Get-ADUser cmdlet below.
$ADUserGroup = Get-ADUser -SearchBase 'OU=Users,DC=Company,DC=com' | Sort-Object Name
foreach ($User in $ADUserGroup) {
$listBox.Items.Add($User.Name) | Out-Null
}
$result = $form.ShowDialog()
$selectedIndex = $listBox.SelectedIndex
# close and remove the form
$form.Dispose()
# handle the results
if ($result -eq [System.Windows.Forms.DialogResult]::OK -and $selectedIndex -ge 0) {
# store the selected AD user object in variable $Employee and do the rest of your code with it
$Employee = $ADUserGroup[$selectedIndex]
}
# $Employee now holds the ADUser object with all properties you asked for or is $null if no selection was made

Related

learning powershell, and having some issues with GetADUser with -Filter

So I am having some issues with the below script. The weird part is, when I run the apparently offending part separately by itself, it works fine. It's only when it tries to run in the add_Click function that it throws an error message.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$SearchTermTest=Read-Host -Prompt "Enter First or last name"
$UsersTest = Get-ADUser -Filter "GivenName -eq '$SearchTermTest' -or SurName -eq '$SearchTermTest'"
Write-Output $UsersTest
# Create the form
$SearchForm = New-Object System.Windows.Forms.Form
$SearchForm.Text = "Search User"
$SearchForm.Size = New-Object System.Drawing.Size(500,500)
# Create the label and textbox for user input
$FirstLastNameLabel = New-Object System.Windows.Forms.Label
$FirstLastNameLabel.Location = New-Object System.Drawing.Size(20,20)
$FirstLastNameLabel.Text = "Enter First or Last Name"
$SearchForm.Controls.Add($FirstLastNameLabel)
$InputTextBox = New-Object System.Windows.Forms.TextBox
$InputTextBox.Location = New-Object System.Drawing.Size(150,20)
$SearchForm.Controls.Add($InputTextBox)
# Create the DataGridView
$DataGridView1 = New-Object System.Windows.Forms.DataGridView
$DataGridView1.Location = New-Object System.Drawing.Size(20,50)
$DataGridView1.Size = New-Object System.Drawing.Size(450,400)
$SearchForm.Controls.Add($DataGridView1)
# Create the search button
$SearchButton = New-Object System.Windows.Forms.Button
$SearchButton.Location = New-Object System.Drawing.Size(280,20)
$SearchButton.Size = New-Object System.Drawing.Size(100,20)
$SearchButton.Text = "Search"
$SearchButton.Add_Click({
$SearchTerm = $InputTextBox.Text
$Users = Get-ADUser -Filter "GivenName -eq '$SearchTerm' -or SurName -eq '$SearchTerm'"
$DataGridView1.DataSource = $null
$DataGridView1.DataSource = $Users
$DataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells)
})
$SearchForm.Controls.Add($SearchButton)
$SearchForm.ShowDialog()
Get-ADUser : The search filter cannot be recognized
At C:\Users\ad_forrest.vasilinda\Desktop\user password search.ps1:36 char:14
+ ... $Users = Get-ADUser -Filter "GivenName -eq '$SearchTerm' -or SurNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ADUser], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:8254,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Error message is above. Any ideas? I am at my wits end honestly. I have the exact same GetADUser statement running with the same filter at the beginning of the script with no errors, its only in the add_click function it throws it.
Update -- changing some of the variable names to be more descriptive and not match any of the reserved names now only causes the error to pop up when the field is left blank. However, now when there is text in the field and I push the button, nothing happens.
I was getting a blank screen as well so I changed it a bit and added some error checking. Now you can search an empty string or part of the name. I changed the filter to -Like so you get better results.
I added a variable $properties so you can add/remove the AD attributes you would like to use.
Cheers
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Properties to display
$properties = "GivenName","SurName","Mail","UserPrincipalName","Company"
# Create the form
$SearchForm = New-Object System.Windows.Forms.Form
$SearchForm.Text = "Search User"
$SearchForm.Size = New-Object System.Drawing.Size(800,500)
# Create the label and textbox for user input
$FirstLastNameLabel = New-Object System.Windows.Forms.Label
$FirstLastNameLabel.Location = New-Object System.Drawing.Size(20,20)
$FirstLastNameLabel.Text = "Enter First or Last Name"
$SearchForm.Controls.Add($FirstLastNameLabel)
$InputTextBox = New-Object System.Windows.Forms.TextBox
$InputTextBox.Location = New-Object System.Drawing.Size(160,20)
$SearchForm.Controls.Add($InputTextBox)
# Create the DataGridView
$DataGridView1 = New-Object System.Windows.Forms.DataGridView
$DataGridView1.Location = New-Object System.Drawing.Size(20,50)
$DataGridView1.Size = New-Object System.Drawing.Size(750,400)
$dataGridView1.ColumnCount = $properties.count
$dataGridView1.ColumnHeadersVisible = $true
$i=0
$properties | %{
$dataGridView1.Columns[$i++].Name = $_
}
$SearchForm.Controls.Add($DataGridView1)
# Create the search button
$SearchButton = New-Object System.Windows.Forms.Button
$SearchButton.Location = New-Object System.Drawing.Size(280,20)
$SearchButton.Size = New-Object System.Drawing.Size(100,20)
$SearchButton.Text = "Search"
$SearchButton.add_click({
$SearchTerm = ($InputTextBox.Text).trim()
$filter = "*"
if ($searchTerm) {
$filter = "GivenName -like '*$SearchTerm*' -or SurName -like '*$SearchTerm*'"
}
[array]$users = Get-ADUser -Filter $filter -Properties $properties | select $properties
$DataGridView1.Rows.Clear()
if ($users) {
$users | %{
$DataGridView1.Rows.Add($_.psobject.Properties.value)
}
}
$DataGridView1.AutoResizeColumns([System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells)
})
$SearchForm.Controls.Add($SearchButton)
$SearchForm.ShowDialog()

I am wanting to return to a previous funtion in Powershell, to rectify with user error if a variable is met?

The current script is as follows;
$HN = hostname
$DN = Get-ADComputer -identity $HN -Properties DistinguishedName | select-object -ExpandProperty DistinguishedName
#*
$OU = 'OU=Workstations,DC=$domain,DC=$domain,DC=$domain'
[array]$A = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope OneLevel -Filter * | Select-Object -ExpandProperty Name
[array]$DropDownArray = $A | Sort-Object
function Return-DropDown {
if ($DropDown.SelectedItem -eq $B){
$DropDown.SelectedItem = $DropDown.Items[0]
$Form.Close()
}
else{
$Form.Close()
}
}
function SelectGroup{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 600
$Form.height = 200
$Form.Text = ”DropDown”
$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(140,10)
$DropDown.Size = new-object System.Drawing.Size(300,80)
ForEach ($Item in $DropDownArray) {
[void] $DropDown.Items.Add($Item)
}
$Form.Controls.Add($DropDown)
$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,40)
$DropDownLabel.Text = "Select Group:"
$DropDown.Font = New-Object System.Drawing.Font("Calibri",15,[System.Drawing.FontStyle]::Bold)
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(140,50)
$Button.Size = new-object System.Drawing.Size(150,50)
$Button.Text = "Select an Item"
$Button.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)
$form.ControlBox = $false
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(290,50)
$Button.Size = new-object System.Drawing.Size(150,50)
$Button.Text = "Finish"
$Button.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$Button.Add_Click({Move-ADObject -Identity "$DN" -TargetPath "$OU" | Return-DropDown})
$form.Controls.Add($Button)
$form.ControlBox = $false
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
$B = $dropdown.SelectedItem
return $B
}
$B = SelectGroup
I would like to develop this tool and add as an aditional option to return to the begining of the previous function;
$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(290,50)
$Button.Size = new-object System.Drawing.Size(150,50)
$Button.Text = "Back"
$Button.Font = New-Object System.Drawing.Font("Calibri",11,[System.Drawing.FontStyle]::Bold)
$Button.Add_Click({Return to #* })
$form.Controls.Add($Button)
$form.ControlBox = $false
Not sure how to achieve this, hoping to find help on here.
I have looked at loops and breaks but nothing seems to fit or that i can adapt to achieve this.
If you're looking for simple repetition of the form function, you could do something like this (unless your tool hides the PowerShell window).
Do {
# Move these lines from #*
$OU = 'OU=Workstations,DC=$domain,DC=$domain,DC=$domain'
[array]$A = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope
OneLevel -Filter * | Select-Object -ExpandProperty Name
[array]$DropDownArray = $A | Sort-Object
$B = SelectGroup
#{... Do Work on $B, if desired ...}
$Stop = Read-Host -Prompt 'Do you want to stop?'
} Until ($Stop -match '(Y|y|Yes|YES|yes)')
Otherwise you'll need to alter your "return-dropdown" function to not close your form and implement your "back" button another way.

How can I get rid of extra characters in TextBox?

The intent of this form is to retreive first a username then use that username to retreive an email address. it does get the address but it also retreives other characters as well. textbox3 ends up reading #{EmailAddress=first.last#domain.com}. I have tried trimming characters but that did not work.
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<#
.NAME
Template
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 100
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(61,52)
$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.multiline = $false
$TextBox2.width = 100
$TextBox2.height = 20
$TextBox2.location = New-Object System.Drawing.Point(219,52)
$TextBox2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox3 = New-Object system.Windows.Forms.TextBox
$TextBox3.multiline = $false
$TextBox3.width = 100
$TextBox3.height = 20
$TextBox3.location = New-Object System.Drawing.Point(61,130)
$TextBox3.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "button"
$Button1.width = 60
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(228,127)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Button1.Add_Click({getacctname})
$Form.controls.AddRange(#($TextBox1,$TextBox2,$TextBox3,$Button1))
#region Logic
function getacctname {
$fname = $TextBox1.Text
$lname = $TextBox2.Text
$User.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" |
Select-Object -ExpandProperty 'SamAccountName' |
Out-Gridview -Title 'Windows Logon' -PassThru
$TextBox3.Text = Get-ADUser -identity $User.text -Properties * | select EmailAddress
}
#endregion
[void]$Form.ShowDialog()
There are two issues with your code:
As explained in a comment, you need to get the value from the EmailAddress property of your object, otherwise, because the .Text property value can only be a string, what you will see as a result is a string representation of a PSCustomObject.
The assignment of $User.Text is invalid and will produce an error unless you're not showing us your actual code. The $User variable not defined hence cannot have a .Text property. Trying to assign anything to it will produce an error such as:
The property 'Text' cannot be found on this object. Verify that the property exists and can be set.
How should your function to solve both issues:
function getacctname {
$fname, $lname = $TextBox1.Text.Trim(), $TextBox2.Text.Trim()
$TextBox3.Text = Get-ADUser -Filter "GivenName -eq '$fname' -and SurName -eq '$lname'" -Properties mail |
Select-Object SamAccountName, mail |
Out-Gridview -Title 'Windows Logon' -PassThru |
Select-Object -ExpandProperty mail
}
As aside, I would recommend you to make $TextBox3 a ReadOnly TextBox:
$TextBox3.ReadOnly = $true

Powershell if else statement

I am having some difficulties with my Powershell script. With this script I am able to enable disabled AD accounts. It works, but I am receiving the wrong output. Accounts got enabled, but still receive the output from the else statement 'Account has not been enabled'. Anyone who can help me? Thanks!
Add-Type -AssemblyName System.Windows.Forms
$SystemInfoForm = New-Object System.Windows.Forms.Form
$SystemInfoForm.ClientSize = "300,100"
$SystemInfoForm.Text = "Enable AD Accounts"
$SystemInfoForm.BackColor = "#ffffff"
$SystemInfoForm.StartPosition = "CenterScreen"
$objIcon = New-Object system.drawing.icon ("C:\Temp\System Info.ico")
$SystemInfoForm.Icon = $objIcon
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the disabled AD account below:'
$SystemInfoForm.Controls.Add($label)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$textBox.Text = "Enter AD account..."
$SystemInfoForm.Controls.Add($textBox)
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,70)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$okButton.Add_Click(
{
$Username = $textBox.Text
if (Search-ADAccount -AccountDisabled | Where-Object {($_.SamAccountName -eq "$Username")} | Enable-ADAccount)
{
[System.Windows.MessageBox]::Show("$Username has been enabled.")
}
else
{
[System.Windows.MessageBox]::Show("$Username has not been enabled.")
}
}
)
$SystemInfoForm.Controls.Add($okButton)
[void]$SystemInfoForm.ShowDialog()
Regards,
Ralph
Enable-ADAccount doesn't return any output by default, so the entire pipeline expression:
Search-ADAccount -AccountDisabled | Where-Object {($_.SamAccountName -eq "$Username")} | Enable-ADAccount
... will evaluate to nothing - and all of that nothing evaluates to $false in your if condition.
Use a try/catch block to catch errors from Enable-ADAccount and then alert the based on that:
try {
Search-ADAccount -AccountDisabled | Where-Object {($_.SamAccountName -eq "$Username")} | Enable-ADAccount -ErrorAction Stop
# We got this far because Enable-ADAccount didn't throw any errors
[System.Windows.MessageBox]::Show("$Username has been enabled.")
}
catch {
[System.Windows.MessageBox]::Show("$Username has not been enabled.")
}
Alternatively use the -PassThru switch with Enable-ADAccount to have it return the account, then inspect that:
$enabledAccount = Search-ADAccount -AccountDisabled | Where-Object {($_.SamAccountName -eq "$Username")} | Enable-ADAccount -PassThru
if($enabledAccount.Enabled){
[System.Windows.MessageBox]::Show("$Username has been enabled.")
}
else {
[System.Windows.MessageBox]::Show("$Username has not been enabled.")
}

Powershell: How to get powershell to give you the option to select the right user when you search with Known name

I have creates a Powershell script that takes a display name from a CSV looks up there username and then adds them to a security group in AD.
The problem is people with the same Display name. My script when it hits the same display name it will just add every user name with that display name.
I would like an option when it hits a name that returns multiple username that it displays an option that allows someone to pick the right username then add them to the security group.
I am fairly new to PowerShell and have come a bit stuck at this point so any help is greatly appreciated.
Import-Module ActiveDirectory
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Security Group Tool"
$Form.Size = New-Object System.Drawing.Size(390,150)
$Form.StartPosition = "CenterScreen"
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Icon = New-Object System.Drawing.Icon("H:\test\favicon.ico")
$Form.Icon = $Icon
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10, 10)
$label.Size = New-Object System.Drawing.Size(400, 15)
$label.Text = "Please enter The name of the Security Group You want to add users too"
$Form.Controls.Add($label)
$textbox = New-Object System.Windows.Forms.TextBox
$textbox.Location = New-Object System.Drawing.Size(10,50)
$textbox.Size = New-Object System.Drawing.Size(240,40)
$Form.Controls.Add($textbox)
$test = {
$secgrp = $textbox.Text
$Sam = #()
$names = Import-Csv "H:\test\Groups2.csv"
foreach ($name in $names.DisplayName) {
$Sam += Get-ADUser -Filter { Name -like $name } -Properties SamAccountName | Select-Object SamAccountName
}
$User = $Sam
foreach ($User in $User) {
Add-ADGroupMember -Identity $secgrp -Members $User
}
}
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(260,45)
$OKButton.Size = New-Object System.Drawing.Size(75,30)
$OKButton.Text = "OK"
$OKButton.Add_Click($test)
$Form.Controls.Add($OKButton)
$Form.Add_KeyDown({
if ($_.KeyCode -eq "Enter") {
& $test
}
})
$Form.Add_KeyDown({
if ($_.KeyCode -eq "Escape") {
$Form.Close()
}
})
$Form.TopMost = $True
$Form.Add_Shown({ $Form.Activate() })
[void] $Form.ShowDialog()
You can check the number of Users returned by Get-ADUser by using the Count property. This will tell you how many objects (users) were returned.
If there is more than 1 user, you can use Out-GridView to display a popup dialogue to select the result you want from the list:
By default this allows multiple selections, but adding -OutputMode Single will then only allow a single selection to be chosen.
Your script can be updated like this:
$test = {
$secgrp = $textbox.Text
$Users = New-Object System.Collections.ArrayList
$names = Import-Csv "H:\test\Groups2.csv"
foreach ($name in $names.DisplayName) {
$ReturnedUser = Get-ADUser -Filter { Name -like $name } -Properties SamAccountName | Select-Object -ExpandProperty SamAccountName
if ($ReturnedUser.count > 1) {
$SelectedUser = $ReturnedUser | Out-GridView -Title "Multiple Users have matched, select User to process" -OutputMode Single
$null = $Users.Add($SelectedUser) #this syntax surpresses the .Add() from displaying the index of each item added
}
else {
$null = $Users.Add($ReturnedUser)
}
}
foreach ($User in $Users) {
Add-ADGroupMember -Identity $secgrp -Members $User
}
}