When you try to run a PowerShell script or command with the Export-CSV cmdlet on your Windows 11 or Windows 10 client machine or Windows Server in a domain, you may get Access to the path is denied output. This post offers applicable fixes to the issue.
The error snippet will read something similar to the following output depending on the syntax and parameters specified:
Export-csv : Access to the path ‘C:\export.csv’ is denied.
At line:1 char:14
+ get-process | Export-csv -Path “C:\export.csv” -NoTypeInformation
This error is most likely to occur predominantly due to the following reasons.
- You’re running PowerShell without administrator permissions.
- Incorrect code or syntax of the script that may have extra spaces in the path, like a forward dash or anything else that shouldn’t be there.
- Spelling errors in the PowerShell command.
PowerShell Export-CSV access to the path is denied
If you get the Access to the path is denied error output when you run the PowerShell Export-CSV cmdlet on a Windows client or server machine, then the easy fixes we have presented below in no particular order can be applied to resolve the issue and have the command execute as intended.
- Run the Export-CSV command in PowerShell elevated console
- Change the location of the Export-CSV file/folder
- Name your Export-CSV file
Let’s see these fixes in detail. Make sure that Windows PowerShell script execution is enabled on the machine.
1] Run the Export-CSV command in PowerShell elevated console
As you can see from the lead-in image above, the Export-CSV access to the path is denied error is displayed when the command is run in PowerShell without admin privilege. So, check that this isn’t the case for you. If not, then proceed with the next suggestion; otherwise, open PowerShell or Windows Terminal in elevated mode and then run the command making sure that there are no spelling errors in the command, and in addition, the code or syntax is correct — for example, check for and remove extra spaces in the path, a forward dash or any character that shouldn’t be present in the syntax.
2] Change the location of the Export-CSV file/folder
This fix requires you to point the export to write to the same folder that the scripts are located in. This builds from the Export-CSV requiring admin privilege or elevated PowerShell console to successfully execute as described above. It’s imperative to point out that the elevation requirement is only with system directories like:
- C:\Program Files
- C:\Program Files (x86)
The above directories are standard for all users and changing their contents would effectively change the Windows environment for other users too as Windows was built for multiple users (regardless of if it will only be set up for one user) and Windows rely on the integrity of these directories. So, any changes or modifications to these directories could have serious implications for the OS performance and potentially affect other users.
That said if you have a custom directory, adding files to it won’t require elevation. The
C:\Temp directory is just another directory like that. So, in summary, any changes that affect other user directories or the Windows environment will require admin rights – anything else won’t.
Read: File cannot be loaded because running scripts is disabled on this system PowerShell error
3] Name your Export-CSV file
For this fix, let’s assume you’re trying to run the following script and the error occurs:
Get-AzureADApplication -all true | Export-CSV -path F:\ADpp
In this case, to resolve the issue, you need to name your CSV file as follows:
Export-CSV -Path "F:\ADpp\2022-12-05_AzureADApps.csv" -NoTypeInformation
I hope this post helps you!
How do I export Active Directory users to CSV files using PowerShell?
To export Active Directory users to CSV with PowerShell, below are the required steps:
- Get-ADUser PowerShell Command.
- Export to CSV command.
- Export specific user attributes.
To bulk import users into Active Directory using PowerShell, below are the required steps:
- Create a CSV file featuring your users’ data.
- Modify PowerShell Script using the command for bulk user creation in Active Directory.
- Run the Script.
How do I run a PowerShell script without restrictions?
If you want to run a script that does not have the required parameters and does not return output, you don’t need to change the execution policy. Instead, you can use the following command to bypass the restrictions.
PowerShell.exe -File "FILENAME" -ExecutionPolicy Bypass
To unblock a PowerShell script, you can run the first command for a single file and the second command below for multiple files.
Unblock-File -Path C:\Users\User01\Documents\Downloads\NameOfPowershellScript
dir C:\Downloads\*PowerShell* | Unblock-File
Also read: How to run .sh or Shell Script file in Windows.