# Search in csv file

**URL:** <https://forums.powershell.org/t/search-in-csv-file/16252>\
**Category:** PowerShell Help\
**Created:** [April 15, 2021, 10:27am UTC](https://forums.powershell.org/t/search-in-csv-file/16252 "2021-04-15T10:27:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kapgep](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Kapgep](https://forums.powershell.org/u/Kapgep)\
**Post date:** [April 15, 2021, 10:27am UTC](https://forums.powershell.org/t/search-in-csv-file/16252/1 "2021-04-15T10:27:10Z")

</div>

Is it possible to somehow implement a search in csv as follows: if the value “Fitter” is in csv, then copy the entire line with this value into a new csv file.

---

<div class="post-metadata">

**Author:** ![metablaster](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/metablaster/32/1224_2.png) [@metablaster](https://forums.powershell.org/u/metablaster)\
**Post date:** [April 15, 2021, 12:21pm UTC](https://forums.powershell.org/t/search-in-csv-file/16252/2 "2021-04-15T12:21:53Z")

</div>

> [@Kapgep](#):
>
> if the value “Fitter” is in csv, then copy the entire line with this value into a new csv file.

For example:

```auto
# first define your "filter", what are you searching for:
[regex] $Filter = "Enter your search string here"

# Specify CSV file location
$File = "$pwd\sample.csv"

# Get file line by line and grab all lines that match
Get-Content $File | Where-Object { $Filter.IsMatch($_) }

```

This will get all lines that match your search pattern.

If you want to get first match and ignore the rest then replace 3rd line with this:

```auto
Get-Content $File | ForEach-Object {
    if ($Filter.IsMatch($_))
    {
        # Output first match
        Write-Output $_

        # Stop processing
        break
    }
}

```

Keep in mind that “Filter” is regex pattern here, if your pattern contains regex special characters then escape them, for example:

```auto
[regex] $Filter = [regex]::Escape("Enter your pattern here")

```

---

<div class="post-metadata">

**Author:** ![Kapgep](https://avatars.discourse-cdn.com/v4/letter/k/5f8ce5/32.png) [@Kapgep](https://forums.powershell.org/u/Kapgep)\
**Post date:** [April 16, 2021, 3:37am UTC](https://forums.powershell.org/t/search-in-csv-file/16252/3 "2021-04-16T03:37:08Z")

</div>

I have implemented as follows:

```auto
$ Files = "C:\source file.csv"
Get-Content $ Files | Select-String "search value" | Export-Csv "C:\Target file.csv" -Encoding UTF8 -Delimiter ";"

```

But, for some reason, a lot of unnecessary columns / values appear:

```auto
"IgnoreCase";"LineNumber";"Line";"Filename";"Path";"Pattern";"Context";"Matches"

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:26pm UTC](https://forums.powershell.org/t/search-in-csv-file/16252/4 "2024-05-16T20:26:42Z")

</div>

Closed on May 16, 2024, 8:26pm UTC


