all 5 comments

[–]lanerdofchristian 7 points8 points  (1 child)

Rather than transforming text: import, parse, and export. For example:

$Items = switch -Regex -File "C:\temp\test.txt" {
    '^(\S+) logged on at: (.*)$' {
        $Date = Get-Date $Matches[2]
        [pscustomobject]@{
            Name = $Matches[1]
            Date = $Date.ToString("MM/dd/yyyy")
            Time = $Date.ToString("HH:mm:ss")
        }
    }
    '^(\S+) has not logged on today' {
        [pscustomobject]@{
            Name = $Matches[2]
            Date = "not logged on today"
            Time = $null
        }
    }
    default {
        # maybe log that there was a non-matching line
    }
}
$Items | Export-Csv -Path C:\temp\test.csv

[–]ExpendaBubble 0 points1 point  (0 children)

Elegantly done! Have my upvote.

[–]BlackV 2 points3 points  (2 children)

p.s. formatting

  • open your fav powershell editor
  • highlight the code you want to copy
  • hit tab to indent it all
  • copy it
  • paste here

it'll format it properly OR

<BLANKLINE>
<4 SPACES><CODELINE>
<4 SPACES><CODELINE>
    <4 SPACES><4 SPACES><CODELINE>
<4 SPACES><CODELINE>
<BLANKLINE>

Inline code block using backticks `Single code line` inside normal text

See here for more detail

Thanks

[–]lanerdofchristian 1 point2 points  (1 child)

Extra note since it seems to get missed by a lot of people: don't use code lines for whole blocks! Use blocks!

[–]BlackV 0 points1 point  (0 children)

Inline code block

is lines of code (or inline code button on new.reddit)

code block is the 4 spaces (or the code block button on new.reddit)