0

First I'm new to PowerShell scripting.

I created a script that the script should greet me with the date.

...

$heute=(Get-Date).ToShortDateString()
$name=$env:UserName
$sven=if ($name -eq 'T1122') {Write-Host 'Sven Kraemer')
Write-Output "Today is" $heute, $sven

...

The output of $env:UserName = T1122 but I does not want to see T1122 as salution but my real name.

T1122 should change into my real name. If this kind of string is T1122 then write my real name.

How I can do it? Thank for your help.

  • Mathias' answer explains the problem well and shows the solution. In short: [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or `Write-Output $value`, though that's rarely needed); see [this answer](https://stackoverflow.com/a/60534138/45375) – mklement0 Jun 05 '21 at 19:34

1 Answers1

1

Write-Host writes output directly to the host application (in the case of powershell.exe, the console screen buffer). Remove it and the string value Sven Kraemer will end up assigned to $sven instead:

$sven = if ($name -eq 'T1122') {'Sven Kraemer')
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206