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

Replace string in an imported CSV file / multiple conditions

I need to import a CSV file and then replace the string domain\username AND the string domain\login with the word SSO.

I know that replacing only domain\username would be:

(Get-Content $ImportCPUFile) |
    ForEach-Object { $_ -replace '"domain\username"', '"sso"' } |
    Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii

But how to make an OR statement in the ForEach-Object function for domain\username and domain\login? I tried:

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

(Get-Content $ImportCPUFile) |
    ForEach-Object {$_ -replace '"domain\username"', '"sso"' -or $_ -replace '"domain\login"', '"sso"'} |
    Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii

But it returns only TRUE statements.

>Solution :

Try RegEx or ‘|’

(Get-Content $ImportCPUFile) |
ForEach-Object { $_ -replace '"domain\\username"|"domain\\login"', '"sso"' } |
Out-File -FilePath CSV-cleaned.csv -Force -Encoding ascii
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