1

we've got a script here what we're using to display groups and then the users who are in the groups. The script is working and getting us the information in the output. Here is the script:

Clear-Host
Write-Host "Group Query & Edit Tool." -ForegroundColor Yellow
Start-Sleep -Seconds 2
Start-Process "\\capeplc.net\IT-GroupData\Onsite Group Memberships\GBSSOnsiteGroupList.txt"
$Group = Read-Host "Enter the name of the group you require memberships pulling from"
Write-Host " "
Write-Host "Users who are members of $Group :"
Get-ADGroupMember -Identity $Group | Select Name,SAMAccountName -Wait
Start-Sleep -Seconds 1
$GroupRemove = Read-Host "Do you want to remove anyone from $Group ?"
if ($GroupRemove -eq "Y"){

$User = Read-Host "Enter the username of the user who needs removing 
(SAMAccountName)"
Remove-ADGroupMember -Identity $Group -Members $User

}
else {

Write-Host "Ending script..."
exit

}

The output however displays the Name & SAMAccountName underneath the entire script after where it says ending script. I want this to display between the write-host which says "Users who are members of $Group" and the $GroupRemove = Read-Host "Do you want to remove anyone from $Group"

Can anyone advise what I'm doing wrong in the script for why it's displaying in the wrong place in the output?

Here is the output for reference:

Group Query & Edit Tool.
Enter the name of the group you require memberships pulling from: GBSS-Onsite-Admin-Aldborough

Users who are members of GBSS-Onsite-Admin-Aldborough :

Do you want to remove anyone from GBSS-Onsite-Admin-Aldborough ?: N
Ending script...
Name         SAMAccountName
----         --------------
Test User    Test.User
Brendan
  • 21
  • 1
  • 3
  • Possible duplicate of [Unable to Pause or Sleep after Select-Object](https://stackoverflow.com/questions/34835327/unable-to-pause-or-sleep-after-select-object) – user4003407 Oct 26 '18 at 13:53
  • Urging users to type information for selection isn't very powershell like. IMO more intuitive is using `Out-Gridview` to select/mark and pass that on in the pipeline. –  Oct 26 '18 at 14:09

1 Answers1

0

powershell has a built in delay for anything that is sent to standard output. Write-Host goes to the host, not to standard output. so you can get delays if the display system thinks there may be more of the same type object on the way. i think the delay is 300 ms, but i am not sure of that.

the fix is to do one of the following ...

  • don't mix host output with any other output that is intended to reach the screen [grin] [aka - the hee-haw "don't DO that!"]
  • add an | Out-Host to non-host display items to force the item to the host without going thru the display system delay routine
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26