Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, February 15, 2012 11:45 PM
Hi guys,
This question is mostly speed but also convenience and to gain knowdledge.
START CODE
$test = Hey
$test2 = PowerShell Programmers
$UserInfoToFile = @"
Name: $test
Username: $test2
Password:
"@
$UserInfoToFile | Out-File -FilePath \nobackup\options$\psscripts\create-user\CreateUserInfo\CreateUserInfo.txt -Encoding ASCII
END CODE
If I open the above in notepad content is not showed like:
Name: Hey
Username: PowerShell Programmers
Password:
But is instead showed like this:
Name: HeyUsername: PowerShell ProgrammersPassword:
If I open the same file in Wordpad no prob. content is showed like:
Name: Hey
Username: PowerShell Programmers
Password:
// Would it be possible to write the code so that notepad will show the here-string as typed?
btw. I have also tried with add-content
Thank you very much.
Red Baron
All replies (9)
Thursday, February 16, 2012 12:02 AM ✅Answered | 1 vote
Seems to work fine for me.
$test and $test are strings, and you need to quote those. Are you sure you posted the same code you're running?
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Thursday, February 16, 2012 12:44 AM ✅Answered | 1 vote
Hi Red Baron,
I agree with mjolinor. your code is working fine and giving output in three line. You may find below information useful.
Scenario 1: while scripting, put all value in single line.
$UserInfoToFile = "Name: $test Username: $test2 Password: "
Result: In this case, output will be in single line.
Scenario 2: While scripting, put all value in new line.
$UserInfoToFile = "Name: $test
Username: $test2
Password: "
Result: In this case you will get output in three lines.
Regarding: Out-File & Add-Content
Out-File: If you use Out-File then it will create new file everytime. It means previous data will be overwritten
Add-Content: In this case output will be appended to existing file/data.
So it depends on your requirement, what to use?
Hope this helps...!!!
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful" if this Post helps you.
Thursday, February 16, 2012 12:53 AM ✅Answered | 1 vote
This likely has something to do with line endings. Creating a multi-line here-string in psh v2 gives you line endings with only the newline character. Normally in Windows you want to see 2 characters at the end of lines: carriage return, newline.
Try converting the line endings before sending it to the file:
**
$userInfoToFile -replace '\n', "`r`n" | out-file ...**
Thanks,
-Lincoln
Thursday, February 16, 2012 10:07 PM ✅Answered
Yes my mistake. It works just fine. I found that in ISE using tab would somehow ruin the here-string....all good when the tabulator inserts where deleted.
Sorry for wasting your time ;-)
Thank you!
Red Baron
Thursday, February 16, 2012 1:26 AM | 3 votes
Hi,
$test = "Hey" $test2 = "PowerShell Programmers" $UserInfoToFile = @" Name: $test Username: $test2 Password: "@ $UserInfoToFile | Out-File -FilePath D:\test.txt -Encoding ASCII
This code should work fine and should have three lines in the test.txt file.
Best Regards,
Yan Li
Yan Li
TechNet Community Support
Thursday, February 16, 2012 3:28 AM
Here is a "hex" dump of the output file. You can see lone LF (hex 0A) characters embedded
instead of CR LF pairs (hex 0D 0A)
Thursday, February 16, 2012 5:35 AM
Here is a "hex" dump of the output file. You can see lone LF (hex 0A) characters embedded
instead of CR LF pairs (hex 0D 0A)
00000000h: 4E 61 6D 65 3A 20 48 65 79 0A 55 73 65 72 6E 61 ; Name: Hey.Userna
00000010h: 6D 65 3A 20 50 6F 77 65 72 53 68 65 6C 6C 20 50 ; me: PowerShell P
00000020h: 72 6F 67 72 61 6D 6D 65 72 73 0A 50 61 73 73 77 ; rogrammers.Passw
00000030h: 6F 72 64 3A 0D 0A ; ord:..
Monday, October 28, 2019 10:15 PM
This likely has something to do with line endings. Creating a multi-line here-string in psh v2 gives you line endings with only the newline character. Normally in Windows you want to see 2 characters at the end of lines: carriage return, newline.
Try converting the line endings before sending it to the file:
**
$userInfoToFile -replace '\n', "`r`n" | out-file ...**Thanks,
-Lincoln
Perfect! I knew it was something like this. But I expected it to be an issue with here-string or even the output functions. This makes some of the logging generated from developer produced powershell a lot more useful now.
Tuesday, October 29, 2019 2:12 PM
You're right. Here strings or multi line strings will make "unix text" with no carriage returns (\r), only line feeds (\n), and notepad doesn't display them well.
'hi
how
are
you' -replace '\n','\n' -replace '\r','\r' # only has \n
hi\nhow\nare\nyou