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
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'
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 isDefault
.
Default
Uses the encoding that corresponds to the system's active code page (usuallyANSI
).
-Encoding
Specifies the type of encoding for the target file. The default value isunicode
.
unicode
UsesUTF-16
with the little-endian byte order.