Pressing Enter is Freezing my script

I am working on a script and am stuck. Basically I want a user to search AD and generate a report in.CSV file of the users in a particular group. I will show the script and the error below. Any advice would be most welcome. I know it can be done better and cleaned up but for now, it works. The only issue I see is when I or some user presses enter the script freezes.

Is there a way to take the Enter key out of the equation? If the enter key did nothing and the user had to rely on just the Mouse and form buttons, I think that would be the best case here. Any Ideas?

Thanks <p style="padding-left: 80px;"><# .NOTES =========================================================================== Created with: PowerShell Created on: 6/8/2020 3:51 PM Created by: William Christner Organization: BCBSA Filename: AD Group Report =========================================================================== .DESCRIPTION Search AD groups, create a .CSV report of users in the group. #></p> <p style="padding-left: 80px;">#################################################################### import-module ActiveDirectory ####################################################################</p> <p style="padding-left: 80px;"> Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing</p> <p style="padding-left: 80px;">$form = New-Object System.Windows.Forms.Form $form.Text = 'Select AD Group' $form.Size = New-Object System.Drawing.Size(350, 140) $form.StartPosition = 'CenterScreen'</p> <p style="padding-left: 80px;">$okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(125, 50) $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) $okButton.add_Click({ ###################################################################################################### $GroupName = $textBox.Text $Date = Get-Date -UFormat "%m.%d.%y_%H.%M" $Members = Get-ADGroupMember -Identity $GroupName -Recursive | Select -ExpandProperty SAMAccountName</p> <p style="padding-left: 80px;">Foreach ($Name in $Members) { Get-ADUser $Name -properties * | Select EmailAddress,SAMAccountName,DisplayName,OfficePhone,CanonicalName,Enabled | Export-Csv -append C:\Reports\$($GroupName)_Membership_Detailed_$($Date).csv -NoTypeInformation -Encoding UTF8 } }) #####################################################################################################################</p> <p style="padding-left: 80px;">$cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Point(225, 50) $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)</p> <p style="padding-left: 80px;">$label = New-Object System.Windows.Forms.Label $label.Location = New-Object System.Drawing.Point(10, 20) $label.Size = New-Object System.Drawing.Size(130, 20) $label.Text = 'Please select AD Group:' $form.Controls.Add($label)</p> <p style="padding-left: 80px;"> $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = New-Object System.Drawing.Point(150, 15) $textBox.Size = New-Object System.Drawing.Size(150, 40) $form.Controls.Add($textBox) $textbox.AutoCompleteSource = 'CustomSource' $textbox.AutoCompleteMode = 'SuggestAppend' $textbox.AutoCompleteCustomSource = $autocomplete "awsarchitect", "BHI", "cc_support", "Citrix", "CyberArk", "CyberArk_Admins", "Datastage", "DB2DBA", "Distribution Groups", "DS_Core_Security", "DS_FEPSIU", "ems-test", "FAS_COLDFUSION_DEV", "FEPBlue", "FEPOC_MousePointer", "FogOperations", "GG_AcceletestAdmins", "GG_AcceletestUsers", "GG_Allow LinkedIn", "GG_Allow_AWS", "GG_Allow_Firewall_ERwin", "GG_Allow_Firewall_Hyper-V", "GG_Allow_Firewall_IBM_Websphere", "GG_Allow_Firewall_Loadrunner", "GG_Allow_Firewall_Mainframe", "GG_Allow_Firewall_NEBA_FTP", "GG_Allow_Firewall_nodejs", "GG_Allow_Firewall_Observer_Platform", "GG_Allow_Firewall_R_Selenium", "GG_Allow_Firewall_Safeend_DPA", "GG_Allow_Firewall_SoapUI", "GG_Allow_Lockdown", "GG_Allow_MS_Collab_Sites", "GG_Allow_Skype", "GG_Allow_Website_Menshealth", "GG_Allow_Windows_GINA", "GG_Allow_Wireless", "GG_Ansible_Development", "GG_ApplicationSupport_Users", "gg_bcbsa_sp_audit", "GG_BindAccounts", "GG_Blackberry_SSP", "GG_Chrome_NoPolicies", "GG_Clearwell_Admin", "GG_Container_Admins_Nonprod", "GG_ContentDistributor", "GG_Contingent", "GG_Core_GPO", "GG_Core_GPO_POC", "GG_Core_Mid", "GG_CredSSP_Vuln", "GG_CTX_Allow_Shadow", "GG_DEP_Disabled", "GG_DevFep", "GG_DriveMappings_Test_Group", "GG_Dropbox ", "GG_DUOAlerts", "GG_eDiscovery_Collections", "GG_EMS_Files", "GG_EV_Client11_Pilot", "GG_EV_Phase2_Pilot", "GG_Exchange_CalendarAdmins", "GG_Exchange_ISAdmin", "GG_Exchange_JrAdmin", "GG_Exchange_MidAdmin", "GG_Exchange_PowerShell", "GG_Exchange_SrAdmin", "GG_Exchange_Training", "GG_Extend_ScreenSaver", "gg_fep_contingents", "GG_FEP_PS_Admins", "GG_FEP_SIU", "GG_FEP_SIU_Legal", "gg_fep_sr_staff", "gg_fep_staff", "GG_Firewall_Testing", "GG_Firewall_Triage", "GG_Foglight_Read", "GG_Git-Admins", "GG_Git-Users", "GG_Google_Man", "GG_Google_SU", "GG_GPO-Arch", "GG_IBC_SandBox", "GG_IDN_Pilot", "GG_Infosys", "GG_iSight-SRM", "GG_JumpServers", "GG_L4CERT_Administrators", "GG_Legal_Hold", "GG_Legal_Hold_Pilot", "GG_LocalProfileRedirect_CHG", "GG_LocalProfileRedirect_MDC", "GG_Lost_Devices", "GG_LTDMailbox", "GG_Microsoft_Patch_Alerts", "GG_Mobile_Users_CHG", "GG_Mobile_Users_Execs", "GG_Mobile_Users_MDC", "GG_MSP_Infosys", "GG_MSP_TechMahindra", "GG_MSP_UST", "GG_No_WPAD", "GG_NRS_Extracts_Access", "GG_O365_Migration", "GG_OneDrive_Pilot", "GG_PeopleSoft_Testers", "GG_Proxy_WPAD2", "GG_RDM_ADMIN_NP", "GG_RDM_Admin_p", "GG_RDM_ALL", "GG_RDM_APP_NP", "GG_RDM_BDC", "GG_RDM_BLUE2", "GG_RDM_IPDS", "GG_RDM_ITS", "GG_RDM_LDLA", "GG_RDM_PDSU", "GG_RDM_TREATMENT_CATEGORY", "GG_RDM_USER_NP", "GG_RDM_User_p", "GG_RDM_WEB_SERVICES", "GG_RDP_Users ", "GG_RecordPoint365_Admins", "GG_Remove_ScreenSaver", "GG_RMS_SuperUsers", "GG_Screensaver_Blank", "GG_Screensaver_Presentations", "GG_Screensaver_Windows10_Lockscreen", "GG_SIEM_Log_Captures", "GG_SIEM_Log_Captures_CHG", "GG_SIEM_Log_Captures_MDC", "GG_SIEM_Log_Captures_NGS", "GG_SIEM_Log_Captures_Rollback", "GG_StaffNow", "GG_StateAffairs", "GG_Tableau_Admin ", "GG_UC_VMware_Admin", "GG_VDI_Application_Permissions", "GG_VDI_PEOPLESOFT_REPORTING-FEPprd", "GG_VDIDesktopSupport", "GG_Web_Servers", "GG_WebEx_Users", "GG_Windows10_GPO_Testing", "GG_Windows10_Users", "GG_Wireless_Profile_Testers", "GG_Wireless_Testers", "HPE_IRS", "Hyperion-Users", "HyperV_Servers", "Informatica", "JohnsAwesomeGPOTestingGroup", "mongo_nw_pe", "mongo_rw_dev", "mongo_rw_pv", "NetApp", "NoOutlookNoIE", "NOW_Upgrade_Temp12", "O365", "OKTA", "Projects", "QualityStage", "sco", "SCOM", "SCVMM", "SNOW_Upgrade_Temp_test", "SNOW_Upgrade_Temp11", "SNOW_Upgrade_Temp12", "SNOW_Upgrade_Temp123", "SNOW_Upgrade_Temp1256", "SNOW_Upgrade_Temp132", "SNOW_Upgrade_Temp145", "SNOW_Upgrade_Temp2", "SNOW_Upgrade_Temp3", "SNOW_Upgrade_Temp33", "SNOW_Upgrade_Temp444", "SNOW_Upgrade_Temp555", "SNOW_Upgrade_Temp888", "Specopshe", "SUG_FlashPlayerPilot", "SystemCenter", "Tower", "UCSD", "VCO-Admin", "VMware"| % { $textbox.AutoCompleteCustomSource.AddRange($_) }</p> <p style="padding-left: 80px;">#[void]$listBox.Items.Add('atl-dc-001')</p> <p style="padding-left: 80px;"> $form.Controls.Add($listBox)</p> <p style="padding-left: 80px;">$form.Topmost = $true</p> <p style="padding-left: 80px;">$result = $form.ShowDialog()</p> <p style="padding-left: 80px;">if ($result -eq [System.Windows.Forms.DialogResult]::OK) { $x = $listBox.SelectedItem $x }</p> <p style="padding-left: 80px;"> explorer.exe C:\Reports</p>

Process Frozen when Enter Key is Pressed

This line tells the form what to do on Enter:

$form.AcceptButton = $okButton

Basically, Enter clicks the Ok button. Try remarking it out so that this is not set.

OK Rob, what I have noticed is that when I have a script running in a command shell, and accidentally/inadvertently click the mouse in that shell, it hangs the script until I hit enter again in the shell. Any fix for that in the script? I always have several shells going (I am not a GUI person) and wonder why the script is taking so long only to realize it has stopped.

No windows forms involved.

Thanks Rob.