1

I'm having some issues with a powershell script that creates signatures for Microsoft Outlook. I'm not very familiar with powershell and are attempting to modify an existing script. The problem I have is the encoding in the txt-file for unformatted mails. I'm in Sweden, so it needs to be able to use swedish characters (åäö). The txt-file that the script outputs do contain proper åäö, but when the signature is opened in Outlook, those characters becomes problematic, ä is shown as ä, ö as ö and so on. After a little bit of googling, it seems that Outlook use Windows-1252, but I can't get powershell to output to that encoding.

This is the script as it looks right now;

$stream = [System.IO.StreamWriter] "$FolderLocation\test.txt"
$stream.WriteLine("--- OBS TEST ---")
$stream.WriteLine("Med vänlig hälsning")

$stream.WriteLine(""+$strName+"")

$stream.WriteLine(""+$strTitle+"")
$stream.WriteLine("")

$stream.WriteLine(""+$strCompany+"")

$stream.WriteLine(""+$strStreet+"")

$stream.WriteLine(""+$strPostCode+" "+$strCity+"")

$stream.WriteLine("")

if($strPhone){$stream.WriteLine("Telefon:    " + $strPhone+"")}
if($strMobile){$stream.WriteLine("Mobil:      " + $strMobile+"")}
$stream.WriteLine("")

$stream.WriteLine("E-post:     "+ $strEmail+"")

$stream.WriteLine("Hemsida:    "+ $strWebsite+"")

$stream.close()

The file that this outputs looks perfectly ok when opened in notepad.

I tried this to re-encode the output file into various encoding, but no success;

get-content -path $FolderLocation\test.txt | out-file -filePath $FolderLocation\$strName.txt -encoding UTF8

Any tips on how to solve this?

Penguin616
  • 13
  • 2
  • 2
    Can you try `$stream = [System.IO.StreamWriter]::new("$FolderLocation\test.txt", [System.Text.Encoding]::GetEncoding(1252))` ? – Theo Nov 25 '19 at 15:50

2 Answers2

0

If you want to stick with your StreamWriter approach, heed Theo's advice and explicitly specify the desired Windows-1252 encoding during stream creation:

$stream = [IO.StreamWriter]::new(
     "$FolderLocation\test.txt",          # file path
     $false,                              # do not append, create a new file
     [Text.Encoding]::GetEncoding(1252)   # character encoding
)

However, given how cumbersome the piecemeal writing of lines is, I suggest switching to a single, expandable here-string, which can you pipe as a whole to Set-Content:

@"
--- OBS TEST ---
Med vänlig hälsning
$strName
$strTitle

$strCompany
$strStreet
$strPostCode $strCity

$(
  $lines = $(if ($strPhone)  { "Telefon:    " + $strPhone  }),
           $(if ($strMobile) { "Mobil:      " + $strMobile }),
           ''
  $lines -ne $null -join [Environment]::NewLine
)
E-post:     $strEmail

Hemsida:    $strWebsite
"@ | Set-Content $FolderLocation\$strName.txt  # See note re PowerShell *Core*

In Windows PowerShell, Set-Content defaults to the system's active ANSI code page, assumed to be Windows-1252.

In PowerShell Core, however, which consistently defaults to (BOM-less) UTF-8, you'll have to specify the encoding explicitly:

Set-Content -Encoding ([Text.Encoding]::GetEncoding(1252)) $FolderLocation\$strName.txt
mklement0
  • 382,024
  • 64
  • 607
  • 775
-1

Add-content instead of writeline should work. Add-Content uses Windows-1252 encoding by default. Swedish characters would be stored properly.

add-content test.txt "--- OBS TEST ---"
add-content test.txt "Med vänlig hälsning"

add-content test.txt ""+$strName+""

add-content test.txt ""+$strTitle+""
add-content test.txt ""

add-content test.txt ""+$strCompany+""

add-content test.txt ""+$strStreet+""

add-content test.txt ""+$strPostCode+" "+$strCity+""

add-content test.txt ""

if($strPhone){add-content test.txt "Telefon:    " + $strPhone+""}
if($strMobile){add-content test.txt "Mobil:      " + $strMobile+""}
add-content test.txt ""

add-content test.txt "E-post:     "+ $strEmail+""

add-content test.txt "Hemsida:    "+ $strWebsite+""
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/24684546) – Andreas Nov 26 '19 at 01:50
  • 3
    @Andreas Using add-content instead of .writeline() will encode in Windows-1252 and solve his problem. – js2010 Nov 26 '19 at 04:42