Search notes:

PowerShell: Redirection Operators

The redirection operators (>, >>, 2>, 2>>, 2>&1) send a command's or an expression's output to a file.
With Powershell 5.1, the redirection operators are effectively aliases for the out-file cmdlet.

Encoding

It turns out that files are created with different encoding when using > in PowerShell 5.1 and PowerShell 7.1.
This is demonstrated with the following test script. It creates a file abc.txt and then displays each byte in the file that was created:
'abc' > abc.txt

if ($psVersionTable.psEdition -eq 'Desktop') {
   $bytes = get-content abc.txt -encoding byte
}
else {
   $bytes = get-content abc.txt -asByteStream
}

foreach ($byte in $bytes) {
  ' {0:x2} {1}' -f $byte, [char]$byte
}
Github repository about-PowerShell, path: /language/operator/redirection/encoding.ps1
In «ordinary» PowerShell, the file is created with a little-endian Byte Order Mark (BOM) and each characters is stored as UTF-16, thus requires two(!) bytes:
 ff ÿ
 fe þ
 61 a
 00
 62 b
 00
 63 c
 00
 0d
 00
 0a

 00
PowerShell Core is more reasonable and does not create the file with a BOM and uses UTF-8(?)
 61 a
 62 b
 63 c
 0d
 0a
Because the redirection operators are aliased to out-file, the encoding for the files that are produced with the redirection operators can be controlled with the $psDefaultParameterValues variable:
$psDefaultParameterValues['out-file:encoding'] = 'utf8'
See also encoding in PowerShell.

See also

In order to express smaller than (<) or greater than (>), the comparison operators -lt or -gt should be used.
The redirection operators in bash
operators

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1745385428, '160.79.111.135', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/operator/redirection/index(97): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78