Powershell - Adding a line to a file from a specific line
To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!
Hello,
Having switched from a database-driven CMS site to a Markdown file-based site, I needed to modify what is known as the frontmatter.
A section at the beginning of the Markdown file containing information about the article (date, title, etc.).
The easiest way to do this is to use a collection (collection, arraylist, genericlist) that contains an .insert() method.
Here are a few examples of how to add a line to a specific location in a file (you know the line number where you want to place your text content)
## From an arraylist# File path$Path = 'c:\Path_File'# Content to add$TextToAdd = "lang: en"# List files$Files = Get-ChildItem -path $Path -File# Loop for each fileforeach ( $File in $Files ) {# Load the contents of the file[System.Collections.ArrayList]$FileContent1 = get-content $File.FullName# Add content# 7 corresponds to an addition on the 8th line$FileContent1.Insert(7,$TextToAdd)# Modify the fileSet-Content -Path $File.FullName -Value $FileContent1}
## From a Generic List# File path$Path = 'c:\Path_File'# Content to add$TextToAdd = "lang: en"# List files$Files = Get-ChildItem -path $Path -File# Loop for each fileforeach ( $File in $Files ) {# Load the contents of the file[System.Collections.Generic.List[String]]$FileContent2 = get-content $File.FullName# Add content# 7 corresponds to an addition to the 8th line$FileContent2.Insert(7,$TextToAdd)# Modify the fileSet-Content -Path $File.FullName -Value $FileContent2}
## Transforming the array into a collection# File path$Path = 'c:\Path_File'# Content to add$TextToAdd = "lang: es"# List the files$Files = Get-ChildItem -path $Path -File# Loop for each fileforeach ( $File in $Files ) {# Load the contents of the file$FileContent3 = get-content $File.FullName# Transformation into a collection to obtain the .Insert() method$FileContent3 = {$FileContent3}.invoke()# Add content# 7 corresponds to an addition to the 8th line$FileContent3.Insert(7,$TextToAdd)# Modify the fileSet-Content -Path $File.FullName -Value $FileContent3}
Related links
Powershell - Testing network connectivity and port accessibility
Testing network connectivity and port accessibility with PowershellPowershell - Display network connections (equivalent to netstat)
Display network connections (listening ports, active connections...)Powershell - Testing name resolution (equivalent to nslookup)
Powershell commands to test name resolution (equivalent to nslookup)Powershell - View and manage DNS configuration of network interfaces
Powershell commands to display and manage DNS configuration of network interfacesPowershell - Managing IP configuration of network interfaces
Powershell commands to view and modify the IP configuration of network interfacesPowershell - Managing the status and configuration of network interfaces
Powershell commands to view and modify the status and configuration of network interfaces (disable IPv6, enable/disable an interface)
Follow me on
Support me
Last content
Powershell - Testing network connectivity and port accessibility
Powershell - Display network connections (equivalent to netstat)
Powershell - Testing name resolution (equivalent to nslookup)
Powershell - View and manage DNS configuration of network interfaces
Powershell - Managing IP configuration of network interfaces
Powershell - Managing the status and configuration of network interfaces
Powershell and the Left Hand Side
Powershell - Managing disks, partitions and volumes