Home ยป PowerShell ยป PowerShell Replace Line in File

PowerShell Replace Line in File

Using the PowerShell replace() method or PowerShell replace operator, it can easily replace a line in a file. You can replace the string in a file and save the updated content to the file using the Set-Content cmdlet.

To read the file in PowerShell, we will use the Get-Content cmdlet, and after replacing of line in a file, to save content in a file use the Set-Content cmdlet.

In this article, we will discuss how to replace the line in a file using the PowerShell replace() method or replace operator.

PowerShell Replace Line in File using Replace() method

PowerShell has the Get-Content cmdlet to read the file content. Using the PowerShell String built-in method replace(), we can easily replace a line in a file or replace a text in a file.

# Read the file content using the Get-Content
$filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw

#Print the file content
$filecontent

# Replace a line in a file

$str.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/")

# Save the replace line in a file  

$filecontent.Replace("Refer the link https://shellgeek.com/tag/active-directory-2/","Refer the link Https://ShellGeek.com/") | Set-Content -Path D:\PS\PS-String.txt                  

    

In the above PowerShell script, the Get-Content cmdlet uses the Path parameter to specify the text file to read and uses the Raw parameter to store the entire file content as a string in the variable $filecontent.

To replace a line in a file, use PowerShell replace() method that takes two arguments; string to find and string to replace with found text.

After the replacement of a line in a file, pipe the output to the Set-Content cmdlet to save the file.

The output of the above PowerShell script to replace a line in a file is:

PowerShell Replace line in File
PowerShell Replace line in File

Cool Tip: How to replace special characters in a string in PowerShell!

Replace the line in a file using PowerShell replace Operator

Using the replace operator, we can replace the line in a file. Use the Get-Content file to read the file content and store it in a variable $filecontent.

Use the PowerShell replace operator over the $filecontent to replace the line to search for and the line to replace within its arguments.

# Read the file content using Get-Content
$filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw

$filecontent

# Use replace operator to replace a line in file
$filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link 
 Https://ShellGeek.com' 

In the above PowerShell script, replace operator replaces the line โ€œRefer the link https://shellgeek.com/tag/active-directory-2/โ€ with โ€œRefer the link Https://ShellGeek.comโ€œ

The output of the above PowerShell script after replacing a line in the file is:

PS C:\> $filecontent = Get-Content -Path D:\PS\PS-String.txt -Raw                                                       
PS C:\>                                                                                                                 

PS C:\> $filecontent
Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link https://shellgeek.com/tag/active-directory-2/

PS C:\>                                                                                                                 

PS C:\> $filecontent -replace 'Refer the link https://shellgeek.com/tag/active-directory-2/','Refer the link Https://ShellGeek.com'                                                                                                             

Hello SysAdmin, Welcome to PowerShell Programming.
Let's begin your Active Directory management using the PowerShell journey here.

Refer the link Https://ShellGeek.com

PS C:\>                                                                                                                                                                                                                                                              

Cool Tip: How to replace all text with a wildcard in PowerShell!

Conclusion

I hope the above article on how to replace the line in a file using the PowerShell replace() method or PowerShell replace operator is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.