Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add and remove Lines Powershell Script

this is my script:

foreach ($Prog in $input) {
    (Get-Content $Prog.CamPath) | Foreach-Object {
            $_ # send the current line to output
                 if ($_ -match '!')  {  
                '<139 \Komponente\'
                'IN="4Seiten_fuegen.mpr"'
                'XA="0.0000"'
                'YA="0.0000"'
                'ZA="0.0000"'
                'EM="0"'
                'PR="0"'
                'VA="length 500.0000"'
                'VA="width 500.0000"'
                'VA="rotation 0.0000"'
                'KO="00"'
                '!'
        }
    } | Set-Content $Prog.CamPath
}

It adds those Lines beneth the "!"

The Result ist something like that:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

!
<139 \Komponente\
IN="4Seiten_fuegen.mpr"
XA="0.0000"
YA="0.0000"
ZA="0.0000"
EM="0"
PR="0"
VA="length 500.0000"
VA="width 500.0000"
VA="rotation 0.0000"
KO="00"
!

It works all fine. I just need to remove the first "!"
Ive googled for hours! nothing works.

Could anybody just tell me how to modify my script?
Probably something like

-remove ("!","")
$_remove ('!','')

I dont know where and how….

kind regards

>Solution :

Change your code to only output $_ after you’ve tested whether it contains !:

... |ForEach-Object {
    if ($_ -match '!')  {  
        '<139 \Komponente\'
        'IN="4Seiten_fuegen.mpr"'
        'XA="0.0000"'
        'YA="0.0000"'
        'ZA="0.0000"'
        'EM="0"'
        'PR="0"'
        'VA="length 500.0000"'
        'VA="width 500.0000"'
        'VA="rotation 0.0000"'
        'KO="00"'
    }
    $_
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading