Help me with importing csv file which is downloaded by get-pnpfile from sharepoint

# Input bindings are passed in via param block.

param($Timer)

Import-Module -name ExchangeOnlineManagement

Import-Module -name PnP.PowerShell

$UserName="admin@....onmicrosoft.com"

$SiteURL="https://....sharepoint.com/sites/epsogtest"

$secpasswd = ConvertTo-SecureString '...' -AsPlainText -Force

$o365cred = New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)

Connect-ExchangeOnline -Credential $o365cred

Register-PnPManagementShellAccess -ShowConsentUrl -TenantName ....onmicrosoft.com

Connect-PnPOnline -Url $SiteURL -Credential $o365cred

$file = Get-PnPFile -Url '/sites/epsogtest/Shared%20Documents/csv.csv' -AsString

$file | ConvertTo-Csv

Import-Csv $file |%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}

Import-Csv $file |%{Set-Contact -Identity $_.Name -StreetAddress $_.StreetAddress -City $_.City -StateorProvince $_.StateorProvince -PostalCode $_.PostalCode -Phone $_.Phone -MobilePhone $_.MobilePhone -Pager $_.Pager -HomePhone $_.HomePhone -Company $_.Company -Title $_.Title -OtherTelephone $_.OtherTelephone -Department $_.Department -Fax $_.Fax -Initials $_.Initials -Notes $_.Notes -Office $_.Office -Manager $_.Manager -CountryOrRegion $_.CountryOrRegion}

Disconnect-ExchangeOnline -Confirm:$false

The import-csv $file doesn’t work because it can’t find file any help please
im using azure function, powershell version 7.

Hi pakvaiselis and welcome,

Unless I’m mistaken the problem is that you’ve piped $file to ConvertTo-Csv, but you’ve not assigned that back to $file.
What happens if you change the line:

$file | ConvertTo-Csv

to

$file = $file | ConvertTo-Csv

Btw, it’s not recommended to use aliases in scripts as they make them harder to read. Better to write out Foreach-Object than to use %.

Domas,
Welcome to the forum. :wave:t4:

Without having excperiences with or access to SharePoint …

What exactly is the result of this code line:

… no matter what the answer to this question may be … this

actually does nothing than outputting something to the console. If you want to capture the output you should either use a varaible assignment or an Out-File.

In the following two commands $file is expected to be a file path. Is it one?

And regardless of that I’d recommend to use only one Foreach-Object. That would probably speed up your code a lot.