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

Dustin Krauss picture Dustin Krauss · Mar 10, 2020 · Viewed 11.2k times · Source

I'm getting the information I need out of the line, although when I go to export the information, I get an error.

When I run this code:

get-process | Export-csv -Path "C:\export.csv" -NoTypeInformation

I get this error:

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
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

How can I export to CSV?

Answer

Sid picture Sid · Mar 10, 2020

You need to use an admin console or change the location of the csv file to C:\temp or something. The home drive require admin permissions.

powershell default vs admin

EDIT: The elevation requirement is only with system directories like:

C:\
C:\Windows
C:\Program Files
C:\Program Files (x86)
C:\Users

Notice that you can change C:\Users\<YourUserName> but not any other directories under C:\Users. Windows was built for multiple users even if you may be the only one using it. The above directories are common for all users and changing its contents would effectively change the Windows environment for other users too since windows rely on the integrity of these directories. Allowing a normal user to delete/modify/add files to these could have dangerous results for the OS and other people who use it.

So it tries to makes sure that only an Admin user is authorized to make changes to it. It is also the same reason why you get prompted for admin permissions when installing a software. It is installed to C:\Program Files or C:\Program Files (x86), make changes to the registry and is available to all users of that computer. Same with removing it. If a program does not require to do that, it doesn't require admin privileges. Eg: Portable softwares. Some programs only use the user directory or users temp directory C:\Users\<Username>\AppData\Local\Temp. Such programs are only available to the user that installed it.

If you have a custom directory like C:\myGames, adding files to it wont require elevation. So C:\Temp is just another directory like that. You will also notice that C:\ will let a normal user create a folder but not a file.

SUMMARY: Any changes that affect other user directories or the windows environment will require admin rights. Others don't.