Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Powershell script to extract text between a keyword

$
0
0

I am looking to extract data from a txt file and output it to multiple txt files. The code I have is updated from another forum member help

$output_file = "C:\Users\Documents\POWERSHELL\"
$AllText = (Get-Content "C:\Users\Documents\POWERSHELL\HAC.txt") -join "`r`n"
$AllText -Split "(?=HAC \d)"| Where{$_ -match "^(HAC \d+)"} | ForEach{Set-Content -Value $_ -Path $output_file ($Matches[1]+'.txt')}

 

It works without the output file destination and creates the file in the default folder but i want to choose the output location so when i run the code like above i get errors

 

Set-Content : A positional parameter cannot be found that accepts argument 'HAC 01.txt'.
At line:4 char:82
+ $AllText -Split "(?=HAC \d)"| Where{$_ -match "^(HAC \d+)"} | ForEach{Set-Content <<<<  -Value $_ -Path $output_file ($Matches[1]+'.txt')}
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand


Viewing all articles
Browse latest Browse all 6937

Trending Articles