1

Code will not return the results of the Get-ADUser command until after I hit a carriage return at the end of the code

    Write-Host "Please enter the user account name. Ex. jsmith1" -ForegroundColor Yellow -BackgroundColor Black
$Username = Read-Host
"`n"

Write-Host "Is this the correct username?  $Username" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "If Yes Type 1; If No type 2" -ForegroundColor Yellow -BackgroundColor Black
$Continue1 = Read-Host

IF ($Continue1 -eq 1)
{
  Get-ADUser -Server "contoso.com" -filter * -Properties * | Where {$_.SamAccountName -match $Username} | Select DisplayName, SamAccountName, EmployeeID, EmployeeNumber
}
ELSE
{}

Write-Host "Would you like to update the Employee ID and the Employee Number?" -ForegroundColor Yellow -BackgroundColor Black
Write-Host "If Yes Type 1; If No type 2" -ForegroundColor Yellow -BackgroundColor Black
$Continue2 = Read-Host

What am I missing here?

Matthew Wetmore
  • 958
  • 8
  • 18
JRN
  • 269
  • 1
  • 3
  • 19

2 Answers2

2

The Write-Host items are immediately displayed during the execution of your command.

The Get-ADUser results are on the output pipeline.

Normally this allows you to return the value into another script, for example, for further processing. Since you don't assign it or capture the output, it simply goes to a default formatter and displays after everything else before the execution ends.

If you really and truly just want to display the output, you can add | Write-Host to the end of your Get-ADUser call. If you ever want to connect this to another script, you can capture the value to a variable, then both Write-Host, and then Write-Output to add it back on the pipeline.

See also: Understanding the Windows PowerShell Pipeline

Matthew Wetmore
  • 958
  • 8
  • 18
  • 1
    Sorry it took me so long to accept this answer. Perfectly logical and easy to understand explination. – JRN Jul 11 '18 at 12:39
1

I found that you can also just add some formatting to the end and it will output as you would expect, when you expect it. I added this formatting to the end:

| FT -AutoSize

R. Noa
  • 31
  • 4
  • Thank you, this workt for me. My Script uses " | Select-Object .." to display data in columns. " | Write-Host" messed up the format but " | ft -AutoSize" does not. – soulflyman Jan 27 '22 at 14:04