PowerShell : How to replace string in files

Issue

How can we replace text in multiple files and folders quickly if folder name and file name are dynamically change.

Solution

Using PowerShell built-in cmdlets we can easily read and replace any kind of content inside of a text file.

First, we can using Get-ChildItem to get all the files. Because the folder name and file name are dynamical then need to use wildcard in the path as below:

$files = Get-ChildItem ‘D:\test\*\files**.txt’ -Recurse

Then we need to loop the $files to get then content for each file and replace.

$files | %{
(gc $_ -Encoding UTF8) -replace ‘test’,” | Set-Content $_.fullname -Encoding UTF8
}

Powershell copy or move files from multiple folder to another folder

In the daily data processing task, we always need to copy or move files from multiple source place to one destination folder. In this blog, I’ll show you how to do those task quickly and easily via PowerShell.

Sample Code

I am using below code to move all csv files under Archives folder to Source folder. Archives folder has more than thousands sub-folders.

get-childitempath e:\archives\*.csv -recurse | move-item -destination d:\source

Get-childitem

Get-ChildItem by default only can get the items in one or more specified folder, to perform sub-folders copy and move , we need to user parameter -recurse .

Pip the result

To perform loop copy or move, we need to pipe (|) the recurse result first, then we can use move-item and copy-item cmdlets to do copy or move.

Use PowerShell to search file content on windows

In this blog, I will show you how to use powershell Select-String cmdlet to string or words from multiple files on Windows quickly?

Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.

Find matches in special files

This command searches all files with the .php file name extension in the current directory

select-string *.php -pattern function

The output displays the lines in those files that include the specified string.

Find a string in subdirectories

This example searches a directory and all of its subdirectories for a specific text string.

Get-ChildItem -Path C:\inetpub\wwwroot\wordpress -Include *.php -Recurse | Select-String -Pattern ‘function posted_on’

Get-ChildItem uses the Path parameter to specify folder, The Include parameter indicate the file type will be searched The Recurse parameter includes the subdirectories. The objects are sent down the pipeline to Select-String.

Select-String uses Pattern parameter and specifies the string ‘ function posted_on ‘

Find a pattern match with wildcard

Select-String -Path ” C:\inetpub\wwwroot\wordpress\*.php” -Pattern ‘posted_on*’

Powershell script sending email (Gmail)

I have a simple powershell project to do file copy and sync task. Can I get email notification if task has issue, like error message or copied file name. The answer is yes, we can us Net.Mail.SmtpClient to send out email in powershell.

Below is the script:

function status_notification{
param( [string]$body,[string] $subject ,[string] $emailTo)
$email =”[email protected]
$psCred = New-Object System.Net.NetworkCredential(“$email”, ” your email password “);
$smtpServer = “smtp.gmail.com”
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer,587)
$smtp.EnableSsl = $true
$msg.IsBodyHTML = $true
$msg.From = “$email”
$msg.To.Add(“$ emailTo”)
$msg.Subject = “$subject”
$msg.Body = “$body”
$smtp.Credentials = $psCred
$smtp.Send($msg)
}

Install DLL to GAC without Visual Studio

I was working on one SSIS project, everything was good till published to production server.

I was using Newtonsoft.Json in one of package and dll must installed to GAC, unfortunately production server don’t have Visual Studio. Therefore I cannot use gacutil.exe tool

Finally, I found Powershell can register dll to GAC as well.

[System.Reflection.Assembly]::Load(“System.EnterpriseServices,Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”)
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall(“C:\Users\admin\Desktop\Newtonsoft.Json.dll”)