Search code examples
powershellpowershell-4.0

Replace string in an imported CSV file


I need to import a CSV file and then replace full usernames domain\username with username.

The following lines work but I only receive the amended usernames as the output and not the full file.

Could you please advise?

$TestFile = Import-Csv .\file.csv 
$NewFile = $TestFile | ForEach-Object {$_."Username" -replace 'domain\\',''}

Solution

  • You can perform the replace on the string data, then convert it into an object using ConvertFrom-Csv.

    $TestFile = (Get-Content .\file.csv) -replace 'domain\\',''
    $NewFile = ConvertFrom-Csv $TestFile