Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell add text line to multiple files

Tags:

powershell

Powershell experince 3 hours...

Scenario: need to add a line of code to the top of multiple ASPX files in multiple folders and subfolders

Is this possible?

I've figured out how to search for the files but adding that line of code is where I'm stuck. This is what I have, which is not working

Get-ChildItem C:\domain_3 -recurse -include "*.aspx" |
Foreach-Object { 
    Add-Content -Path $targetFile -Value "<%@ Page Language="C#" Inherits="LandingPages.LandingPage" %>"; 
}

Thank you all

like image 575
Unreal1969 Avatar asked May 08 '26 14:05

Unreal1969


1 Answers

$header=@"
"<%@ Page Language="C#" Inherits="LandingPages.LandingPage" %>
"@

Get-ChildItem C:\domain_3 -Recurse -Filter *.aspx | Foreach-Object { 
    $header`n" + (Get-Content $_.FullName | Out-String) | Set-Content -Path $_.FullName
}
like image 150
Shay Levy Avatar answered May 10 '26 06:05

Shay Levy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!