using Write-host multiple times

259 Views Asked by At

I am trying to write a Powershell script that will use the Write-Host commands a couple of times. when i run the script i get the output of the Write-host and then the output from the commands themselves. I've looked and Googled this issue but no joy. here is the script if any one can help?

#Use this script to find all acounts in the External Contractors OU with accounts that have expired and will expire with in the next 365 days

$cred = Get-Credential

Write-Host "Accounts due to expire in the next 365 days" -ForegroundColor red -BackgroundColor white
Search-ADAccount -Server teeis0002.europe.tel.com -Credential $cred `
    -searchbase "OU=External Contractors,OU=Users,OU=xxxxxx,DC=xxxxxx,DC=xxxxxx,DC=xxxxx" `
    -AccountExpiring -TimeSpan 365.00:00:00 -UsersOnly -ResultPageSize 2000 -resultSetSize $null | 
    Select-Object Name, UserPrincipalName, DistinguishedName, AccountExpirationDate | 
    Sort-Object -Property AccountExpirationDate

Write-Host "Currently Expired Accounts"  -ForegroundColor red -BackgroundColor white
Search-ADAccount -Server teeis0002.europe.tel.com -Credential $cred `
    -searchbase "OU=External Contractors,OU=Users,OU=xxxxxx,DC=xxxxxx,DC=xxxxxx,DC=xxxxx" `
    -AccountExpired  -UsersOnly -ResultPageSize 2000 -resultSetSize $null | 
    Select-Object Name, UserPrincipalName, DistinguishedName, AccountExpirationDate | 
    Sort-Object -Property AccountExpirationDate

I looked at using Do While and For each

2

There are 2 best solutions below

3
StefanGreve On

If I understand your question correctly, you want to suppress all the output from the commands with the exception of your Write-Host lines.

In PowerShell, you have a couple of options for that:

  1. You can save a Cmdlet result to variable. This often makes sense if you suspect that you could need that information later in your script anyway, e.g.
# outputs a System.IO.DirectoryInfo object
# which it outputs to the terminal
New-Item -ItemType Directory -Name Test

# captures the output in a variable
# hence, you will see no output in the terminal
$Folder = New-Item -ItemType Directory -Name Test
  1. You can also use [void](Verb-Noun) to keep your terminal screen clean, this casts the output to void. There's actually quite a few different ways to go about this, and while they may be technically not doing all the exact same thing, for all intents and purposes it's doing what you want it to do:
# cast to null
[void](New-Item -ItemType Directory -Name Test)

# redirect output to null
New-Item -ItemType Directory -Name Test > $null

# use a Cmdlet to pipe to null (standard output only)
New-Item -ItemType Directory -Name Test | Out-Null

But sometimes that may not be enough if you also want to suppress error messages (although you should think hard about whether that's something you really want to do but that's besides the point here).

Error messages are expected to written to the error stream. You can read more about streams here.

# just redirect the error stream to null
Verb-Noun 2>$null

# redirect success and error stream to null
Verb-Noun 2>&1>$null

# redirect every stream to null
Verb-Noun *>$null
0
Mathias R. Jessen On

If you want to force the output from a command pipeline to be rendered on screen before anything else happens, pipe to Out-Host:

Write-Host "Message 1"
Search-ADAccount ... |Out-Host

Write-Host "Message 2"
Search-ADAccount ... |Out-Host