Question:
I am trying to get all the lines from an Input file starting with %% and paste it into Output file using powershell.
Used the following code, however I am only getting last line in Output file starting with %% instead of all the lines starting with %%.
I have only started to learn powershell, please help
1 2 3 4 5 6 7 8 |
$Clause = Get-Content "Input File location" $Outvalue = $Clause | Foreach { if ($_ -ilike "*%%*") { Set-Content "Output file location" $_ } } |
Answer:
You are looping over the lines in the file, and setting each one as the whole content of the file, overwriting the previous file each time.
You need to either switch to using Add-Content
instead of Set-Content
, which will append to the file, or change the design to:
1 2 3 4 5 6 7 |
Get-Content "input.txt" | Foreach-Object { if ($_ -like "%%*") { $_ # just putting this on its own, sends it on out of the pipeline } } | Set-Content Output.txt |
Which you would more typically write as:
1 2 |
Get-Content "input.txt" | Where-Object { $_ -like "%%*" } | Set-Content Output.txt |
and in the shell, you might write as
1 2 |
gc input.txt |? {$_ -like "%%*"} | sc output.txt |
Where the whole file is filtered, and then all the matching lines are sent into Set-Content in one go, not calling Set-Content individually for each line.
NB. PowerShell is case insensitive by default, so -like
and -ilike
behave the same.