Petr Synek
Petr Synek

Reputation: 135

Export-Csv doesn't create a file

I need to export CSV data to a CSV file. I have this code:

$Input | where {$AnotherFile.userPrincipalName -notcontains $_.login} |
    Export-Csv -Path "$PSScriptRoot + \output.csv" -Delimiter ";" -NoTypeInformation

In this case, the script ends with this error: Part of path "script root + \output.csv was not found". The script root is correctly written full path to the folder, where the script is located.

I tried also this:

$Input | where {$AnotherFile.userPrincipalName -notcontains $_.login} |
    Export-Csv -Path "output.csv" -Delimiter ";" -NoTypeInformation

In this case, the script seems to finish smoothly (no error is displayed), but the file is not created. I even tried search the disk, if it wasn't created in another directory.

This works perfectly correct:

$Input | where {$File.userPrincipalName -notcontains $_.login}

In this case I get precisely all the data I want, listed on the screen. Which is nice, but useless. What error do I make in the Export-Csv cmdlet?

Upvotes: 0

Views: 3851

Answers (1)

henrycarteruk
henrycarteruk

Reputation: 13207

The path for CSV "$PSScriptRoot + \output.csv" will be evaluated into C:\folder + \output.csv which is likely not your intention.

Remove the spaces and + so it it's just: "$PSScriptRoot\output.csv" this will evaluate to C:\folder\output.csv.

$Input | where {$AnotherFile.userPrincipalName -notcontains $_.login} | Export-Csv -Path "$PSScriptRoot\output.csv" -Delimiter ";" -NoTypeInformation

Upvotes: 1

Related Questions