Why Am I Getting Access Denied When Exporting to C:Temp in PowerShell?

0
4
Asked By CuriousCantaloupe42 On

Hey everyone! I'm having trouble running a PowerShell script to export all my Active Directory groups along with their creation dates and times. Here's the script I'm using:

```powershell
Get-ADGroup -Filter * -Properties whenCreated, whenChanged |
Select Name, DistinguishedName, GroupScope, GroupCategory,
@{Name='Created';Expression={$_.whenCreated}},
@{Name='Modified';Expression={$_.whenChanged}},
ObjectGUID |
Export-Csv -Path C:Temp -NoTypeInformation
```

The script seems to run fine until it hits the line to export to CSV, at which point I get an error saying:

```
Export-Csv : Access to the path 'C:Temp' is denied.
```

I've checked the permissions and I have full access to the folder, I'm a domain admin, and I've also started PowerShell as an administrator. Can anyone suggest what else I might check or what I might be doing wrong?

2 Answers

Answered By CodeCrusader88 On

Another option is to use `$env:temp` for the export path. But keep in mind, I prefer using `[System.IO.Path]::GetTempPath()` nowadays for better compatibility across different systems. Just a thought!

Answered By TechieTurtle1 On

It seems like you're trying to save the CSV directly to the Temp folder without specifying a filename. Instead of just `C:Temp`, try using `C:Tempgroups.csv`. This should fix the access issue you're encountering.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.