Luke Toulmin-Rothe
asked on
Powershell script to replace characters within multiple files
I have created a Powershell script that will replace a character string with another string, this works file along as only one file is in the directory.
This is the script I am using
#Example of PowerShell replacing text
Clear-host
$Location = "C:\Users\luketr\Desktop\t est\*.txt"
$Change = Get-Content $Location
$Change | ForEach-Object {$_ -Replace "'THIRD',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'SECOND',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'FOURTH',", ""} | Set-Content $Location
If I have more than one file in the directory the files are merged.
I have attached the files I am wanting the place the word, SECOND, THIRD and FOURTH in the separate files.
Thanks
Luke
bacotim0.20986.txt
bacotim0.21008.txt
bacotim0.21033.txt
This is the script I am using
#Example of PowerShell replacing text
Clear-host
$Location = "C:\Users\luketr\Desktop\t
$Change = Get-Content $Location
$Change | ForEach-Object {$_ -Replace "'THIRD',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'SECOND',", ""} | Set-Content $Location
$Change | ForEach-Object {$_ -Replace "'FOURTH',", ""} | Set-Content $Location
If I have more than one file in the directory the files are merged.
I have attached the files I am wanting the place the word, SECOND, THIRD and FOURTH in the separate files.
Thanks
Luke
bacotim0.20986.txt
bacotim0.21008.txt
bacotim0.21033.txt
ASKER
Thanks when I run this I get the following error.
Get-Content : A parameter cannot be found that matches parameter name 'Raw'.
At C:\Users\luketr\Desktop\Te st\find and replace.ps1:2 char:28
+ (Get-Content -Path $_ -Raw <<<< -Encoding ASCII) -replace "'(SECOND|THIRD|FOURTH)'," , "" | Set-Content -Path $_
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Mic rosoft.Pow erShell.Co mmands.Get ContentCom mand
Get-Content : A parameter cannot be found that matches parameter name 'Raw'.
At C:\Users\luketr\Desktop\Te
+ (Get-Content -Path $_ -Raw <<<< -Encoding ASCII) -replace "'(SECOND|THIRD|FOURTH)',"
+ CategoryInfo : InvalidArgument: (:) [Get-Content], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Mic
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent thank you very much.
Open in new window