I have a CSV file like below. I want to only fetch [email protected] line like below.
My CSV file:
Report Refresh Date,User Principal Name,Display Name,Is Deleted,Deleted Date,Last Activity Date,Send Count,Receive Count,Read Count,Meeting Created Count,Meeting Interacted Count,Assigned Products,Report Period
2022-10-05,[email protected],User01,False,,2022-10-05,0,0,0,0,0,POWER BI (FREE) DYNAMICS 365 CUSTOMER SERVICE ENTERPRISE MICROSOFT VIVA INSIGHTS OFFICE 365 E3 ENTERPRISE MOBILITY SECURITY E3,30
2022-10-05,[email protected],info,False,,2022-10-05,0,0,0,0,0,POWER BI (FREE) DYNAMICS 365 CUSTOMER SERVICE ENTERPRISE MICROSOFT VIVA INSIGHTS OFFICE 365 E3 ENTERPRISE MOBILITY SECURITY E3,30
2022-10-05,[email protected],user02,False,,2022-10-05,0,0,0,0,0,POWER BI (FREE) DYNAMICS 365 CUSTOMER SERVICE ENTERPRISE MICROSOFT VIVA INSIGHTS OFFICE 365 E3 ENTERPRISE MOBILITY SECURITY E3,30
My desired output:
Report Refresh Date,User Principal Name,Display Name,Is Deleted,Deleted Date,Last Activity Date,Send Count,Receive Count,Read Count,Meeting Created Count,Meeting Interacted Count,Assigned Products,Report Period
2022-10-05,[email protected],info,False,,2022-10-05,0,0,0,0,0,POWER BI (FREE) DYNAMICS 365 CUSTOMER SERVICE ENTERPRISE MICROSOFT VIVA INSIGHTS OFFICE 365 E3 ENTERPRISE MOBILITY SECURITY E3,30
CodePudding user response:
how about this?
# define path to CSV
$CSV = "C:\a.csv"
# import CSV Contents
$CSVContent = Import-Csv $CSV
# go through each entry in the CSV file
foreach ($Entry in $CSVContent){
# if the entry matches a certain criteria
If ($Entry."User Principal Name" -eq "[email protected]"){
# output that entry to console
Write-Output $Entry
}
}
CodePudding user response:
Import-Csv ./input.csv |
Where-Object 'User Principal Name' -eq '[email protected]' |
ConvertTo-Csv
CodePudding user response:
It’s actually possible to do this a one-liner in PowerShell. For the purposes of the exercise, $contoso
is the original file, and $infocontoso
is the file that will contain only the desired records.
Import-CSV -Path $Contoso | Where-Object {$_.'User Principal Name' -eq '[email protected]'} | Export-CSV -Path $infocontoso
CodePudding user response:
#Load csv
$csv = import-csv [path]
#Get entry where User Principal Name eq [email protected]
$entry = $csv | ?{$_.'User Principal Name' -eq '[email protected]'}
The variable $entry contains the related object.
If you need a csv output on the screen:
$entry | convertto-csv
If you need the csv output as file
$entry | export-csv [path]