How to add a new line to an exisiting text file using PowerShell and insert the file name

Chloe Jarman 26 Reputation points
2021-01-26T10:02:58.97+00:00

Hi all,

I'm new to PowerShell and my predecessor had written a scrip to alter text files to make them readable to upload to our cloud system.

He took the file, changed the months "text" into "numbers" and re-saved it in a new folder. The script looks like this:

Get-ChildItem "C:\Users\byveld-m\Desktop\Batch Test\Processed*.txt" | where {$.Name -match ".txt"} | % {((Get-Content $) -replace "`t", ";" -replace "January", "01" -replace "February", "02" -replace "March", "03" -replace "April", "04" -replace "May", "05" -replace "June", "06" -replace "July", "07" -replace "August", "08" -replace "September", "09" -replace "October", "10" -replace "November", "11" -replace "December", "12") | Set-Content "C:\Users\byveld-m\Desktop\Batch Test\Processed\Upload\$($_.Name)"

What I'm trying to figure out is how I can add a header row with the files name in it.

The current txt file is called AHU1_Supply_Temp_RecordedData.txt and displays straight away the following lines:

19 March 2018 14:15:00 10.8300
19 March 2018 14:30:00 10.6500

However, what I need is for the text file to display the following:

AHU1_Supply_Temp_RecordedData
19 03 2018 14:15:00;10.8300
19 03 2018 14:30:00;10.6500

How can I go about achieving this?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

Accepted answer
  1. Titan 206 Reputation points
    2021-01-26T13:34:56.497+00:00

    Hello @Chloe Jarman !
    Does this work for you?

    $srcPath = "C:\Users\byveld-m\Desktop\Batch Test\Processed*.txt"  
    $dstPath = "C:\Users\byveld-m\Desktop\Batch Test\Processed\Upload\"  
    Get-Item -Path $srcPath |  
    ForEach-Object {  
      New-Item -Path $dstPath -Name $_.Name -ItemType "file" -Value "$($_.BaseName)`n" -Force  
      (Get-Content $_) -replace "`t", ";" -replace "January", "01" -replace "February", "02" -replace "March", "03" -replace "April", "04" -replace "May", "05" -replace "June", "06" -replace "July", "07" -replace "August", "08" -replace "September", "09" -replace "October", "10" -replace "November", "11" -replace "December", "12" |  
      Add-Content -Path "$dstPath$($_.Name)"  
    }  
    

    Best whishes

    ---
    If this Answer works for you, accept and upvote it please.


1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-01-26T20:55:02.753+00:00

    Here's another take on this:

    $srcPath = "C:\Users\byveld-m\Desktop\Batch Test\Processed*.txt"
    $dstPath = "C:\Users\byveld-m\Desktop\Batch Test\Processed\Upload\"
    Get-Item -Path $srcPath |
        ForEach-Object {
            $new = New-Item -Path $dstPath -Name $_.Name -ItemType "file" -Value "$($_.BaseName)`n" -Force
            Get-Content $_ |
                ForEach-Object {
                    $s = $_ -split "`t"
                    #  day   month   year    hour  minute second
                    "{0:dd} {0:MM} {0:yyyy} {0:hh}:{0:mm}:{0:ss};{1}" -f [DateTime]$s[0], $s[1] |
                        Add-Content -Path $new.FullName
                }
        }
    
    0 comments No comments