Home > other >  powershell csv unable to search
powershell csv unable to search

Time:01-26

I'm trying to learn basic powershell loops on CSV files. Tried many tutorials or guides online but no avail. Hope you guys can help. Thanks

Here's my CSV file

csv file

Tried to loop through it to look for vpn_name and display the key but seems it won't find what I'm looking for. The path is correct since I tried to display what's inside $c.'vpn_name' displays list from test to test3.

$csv = import-csv -Path "path.csv"
foreach ($c in $csv) {
    $c.'vpn_name' -contains 'test'
}

Hope you guys can help. Really appreciate it. Thanks

CodePudding user response:

Firstly, the csv file format is:

vpn_name,vpn_key
test,lksajdlksja
test1,lksajdlksja
test2,lksajdlksja

Secondly, use the following to search:

   foreach ($c in $csv) {
        $c.'vpn_name' -like '*test*'   //All three rows of the csv file return true.
    }

    foreach ($c in $csv) {
        $c.'vpn_name' -contains 'test'   //Only first row of the csv file return true.
    }

About your another question, please replace

'*$username*'

with

'*' $username '*'
                              
  •  Tags:  
  • Related