0

My PowerShell script:

          $result = foreach ($user in $list) {
              $samname = $user.SamAccountName 
              $manager = $user.Manager
              $name = $user.Name 
              $managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
                  if ($managername.Name -eq $manager) {
                      Write-Output "$manager exists in AD."
                      $dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
                      Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
                  }
                  else {
                      Write-Warning "$manager does not exist in AD."
                  } 
          }
          $result | Out-File -FilePath C:\testfile.txt -Width 500 

$manager = $user.Manager is a list of names from a file. If $manager exists in AD, there will be a write-output "$manager exists in AD."

In order to find $manager in AD, I created the variable $managername, and wrote an if ($managername.Name -eq $manager).

I am able to produce the write-warning in the console, but not write-output. However, in the txt file, it is the opposite. The write-output if message is listed in the txt file, but not the write-warning else message.

What do I write to produce both if and else messages in the console and also the txt file?

Update: Based on the suggestions given, I tried:

I removed $result, replaced Write-Output and Write-Warning, and tested with both Write-Host, then Write-Information. It did not manage to send any message to the txt file:

          foreach ($user in $list) {
              $samname = $user.SamAccountName 
              $manager = $user.Manager
              $name = $user.Name 
              $managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
                  if ($managername.Name -eq $manager) {
                      $if = Write-Host "$manager exists in AD."
                      $dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
                      Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
                  }
                  else {
                      $else = Write-Host "$manager does not exist in AD."
                  } 
          }
          $if, $else | Out-File -FilePath C:\testfile.txt -Width 500 

Appending both if and else write-output messages to a variable shows up in the console, but nothing appeared in the txt file.

          foreach ($user in $list) {
              $samname = $user.SamAccountName 
              $manager = $user.Manager
              $name = $user.Name 
              $managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
                  if ($managername.Name -eq $manager) {
                      $if = Write-Output "$manager exists in AD."
                      $dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
                      Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
                  }
                  else {
                      $else = Write-Output "$manager does not exist in AD."
                  } 
          }
          $if, $else | Out-File -FilePath C:\testfile.txt -Width 500 

Both messages with write-output only will have both if and else messages in the console. How do I output the messages to a txt file though?

          foreach ($user in $list) {
              $samname = $user.SamAccountName 
              $manager = $user.Manager
              $name = $user.Name 
              $managername = Get-ADUser -Filter "Name -eq '$manager'" | Select Name
                  if ($managername.Name -eq $manager) {
                      Write-Output "$manager exists in AD."
                      $dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
                      Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
                  }
                  else {
                      Write-Output "$manager does not exist in AD."
                  } 
          }
865296
  • 3
  • 3

2 Answers2

0

Write-Output writes the specified objects to the pipeline.

It does not explicitly write to the console!

In PowerShell, objects in the pipeline might be:

  • passed on (piped) to a following cmdlet, e.g.:
  Write-Output "$manager exists in AD." |Next-Action
  • assigned to a variable, e.g.:
  $result = Write-Output "$manager exists in AD."
  • by default (in case they aren't piped or assigned), eventually displayed on the console

Note

The Write-Output cmdlet is usually not required as PowerShell sends any object that is not piped and not assigned down to the pipeline by default

 

To explicitly write to the console you might use the Write-Host or the Write-Information cmdlet.

Note

Starting in Windows PowerShell 5.0, Write-Host is a wrapper for Write-Information This allows you to use Write-Host to emit output to the information stream. This enables the capture or suppression of data written using Write-Host while preserving backwards compatibility.

 

In your case, the pipeline is captured by the result = assignment and I guess you actually want to write to both the pipeline line and the console. For this, you might use the Tee-Object cmdlet which saves the command output in a file or variable and also sends it down the pipeline. To print the output to console and use in the pipeline

Tee-Object -InputObject "$manager exists in AD." -FilePath ($IsWindows ? '\\.\CON': '/dev/tty')

Related: #19827 Tee-Object should have a -Console parameter

iRon
  • 20,463
  • 10
  • 53
  • 79
  • Please let me know whether this answers the question by [accepting](https://stackoverflow.com/help/someone-answers) it. – iRon Jun 20 '23 at 10:06
  • I replaced the line "Write-Output "$manager exists in AD."" with Tee-Object "$manager exists in AD." -FilePath ($IsWindows ? '\\.\CON': '/dev/tty') , but the error message says that ? is an unexpected token – 865296 Jun 21 '23 at 12:30
  • Apparently you are using *Windows* PowerShell which doesn't know `$IsWindows` and the **ternary if**, in that case you might simplify the command to just: `Tee-Object -InputObject "$manager exists in AD." -FilePath '\\.\CON'` – iRon Jun 21 '23 at 12:57
0

Write-Host is what you're looking for, writes to the console, without having to capture anything.

Write-Host "$manager exists in AD."
$dname = Get-ADUser -Filter "Name -eq '$manager'" | Select DistinguishedName
Get-ADUser -Filter "SamAccountName -eq '$samname'" | Set-ADUser -Manager $dname
halldk
  • 13
  • 5
  • I guess that the OP also want to write to the pipeline. What else is the `$result =` for? – iRon Jun 20 '23 at 17:01
  • The way it's written, the result isn't printed, or saved anywhere, so I assume it's a throw away, but maybe they're trying to save data into a csv for later? But to write-output with the $result a different way still accomplishes the same thing. I'm operating off of KISS, so just write-host for the desired result. – halldk Jun 20 '23 at 17:09
  • Yes, my intention is to output both the if and else message to a txt file, and also have these if and else messages written in the console. I have already added the missing " $result | Out-File -FilePath C:\testfile.txt -Width 500 " – 865296 Jun 21 '23 at 12:32