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