It write Day of week in date.txt text file but the output text file starts
with two blank line (like Carriage Return) and then : "DayOfWeek : Thursday"
Is there any way to format the output text file so the "DayOfWeek :
Thursday" starts at the first line of the text file (without blank lines)?
Thanks in advance.
Ing. Cosimo Mercuro
Yes, there are annoying blank lines that will show up with the various
format-* and select-object cmdlets, for example.
Quick workaround:
PS>"DayOfWeek: "+(get-date).dayofweek|out-file -Encoding ascii date.txt
Marco
--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com
# if you pipe the formatted objects to Out-String with its
# -Stream switch 'on' you could then filter out the blanks
# through Where-Object:
get-date | format-list DayOfWeek | out-string -stream |
? {$_} | out-file -encoding ascii date.txt
gc date.txt
# another way is to remove the blank lines from the file
# and set the content back to it:
get-date | format-list DayOfWeek | out-file -encoding ascii date.txt
# before
gc date.txt
set-content date.txt (${c:date.txt} | ? {$_}) -encoding ascii
# after
gc date.txt
# to remove all blank lines but leave the top, or the bottom, ones
get-date | format-list DayOfWeek | out-file -encoding ascii date.txt
# remove all blank lines, except for the bottom
(gc date.txt | out-string) -replace '(\r\n)+',"`r`n" -replace '^(\r\n)+'
# remove all blank lines, except for the top
(gc date.txt | out-string) -replace '(\r\n)+',"`r`n" -replace '(\r\n)+$'
--
Kiron
Just for the sake of one more way to do it:
Get-Date | %{"DayOfWeek: $($_.DayOfWeek)"} | out-file -Encoding ascii
date.txt
"DayOfWeek: $((get-date).dayofweek)" | out-file -Encoding ascii date.txt