Search code examples
powershellpowershell-3.0export-csv

Export Powershell Object to CSV - Access Denied


I'm trying to get the permission of the calendar folder on a set user, then export it to a CSV, so we can easily view the permissions.

When I run this command:

Get-Mailbox |
    Get-MailboxPermission |
    Select-Object AccessRights, User, Identity, IsValid |
    Export-Csv -Path 'c:\Users\Connor\Desktop'

I get this error:

Export-Csv : Access to the path 'C:\Users\Connor\Desktop' is denied.
At line:1 char:89
+ ... hts,User,Identity,IsValid | Export-Csv -Path 'c:\Users\Connor\Desktop'
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Export-Csv], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ExportCsvCommand

Solution

  • You need to make sure the PowerShell has write permission to the folder you're trying to write to. You can do this by

    1. Relaunching Powershell as Administrator
    2. Specifying a folder that PS can write to without elevated permissions
      • Try C:\temp\ (plus your filename.csv)
      • Or call Path.GetTempPath in PS with [System.IO.Path]::GetTempPath()

    See Also: Export-Csv - Access to the path 'C:\export.csv' is denied