Home > Blockchain >  Strange format of a table in a txt file
Strange format of a table in a txt file

Time:02-28

In a powershell script, I am creating a txt file:

#Create a logfile
$logfile = "E:\scripts\Password expiration\PasswordExpiryLog.txt"

#Initialize the log file with the date
$date = Get-Date -Format "dd.MM.yyyy HH:mm:ss"
$message= "********************"    $date    "********************"
Add-Content $logfile $message

After that, I do some other things to get some info. When I try to send that info into the TXT file, formatting the info as a table, it writes the information in a way that the information is not in columns, missing the break of line. I mean, if I press enter where the next line should start, it is written in a column.

$Expired | FORMAT-TABLE @{Name='UserPrincipalName';Expression={"$($_.UserPrincipalName)"};align ='left'}, @{Name='rapasswordexpiring';Expression={"$($_.rapasswordexpiring)"};align ='center'}, @{Name='PasswordLastSet';Expression={"$($_.PasswordLastSet)"};align ='left'} -AutoSize | Out-File -Append 'E:\scripts\Password expiration\PasswordExpiryLog.txt'

I have also realized, while writing this message, that between the characters there is always a space

How it looks

How it should look

If I instead of out-file to the same logfile I use a new txt file, the information is written correctly.

$Expired | FORMAT-TABLE @{Name='UserPrincipalName';Expression={"$($_.UserPrincipalName)"};align ='left'}, @{Name='rapasswordexpiring';Expression={"$($_.rapasswordexpiring)"};align ='center'}, @{Name='PasswordLastSet';Expression={"$($_.PasswordLastSet)"};align ='left'} -AutoSize | Out-File -Append 'E:\scripts\Password expiration\table.txt'

Correct

However, I cannot get here the date and time.

Can you tell me what am I doing wrong or how to improve the output? I would be able to use cvs or html file if would be better.

CodePudding user response:

Different default encoding:

-Encoding Specifies the type of encoding for the target file. The default value is Default.

Default Uses the encoding that corresponds to the system's active code page (usually ANSI).

-Encoding Specifies the type of encoding for the target file. The default value is unicode.

unicode Uses UTF-16 with the little-endian byte order.

  • Related