I am trying to run this script through GPO deployed scheduled task:

```
$registryPath = 'HKLM:\Software\CC\PST_Discovery_Script\Already_Run'

if  (!(Test-Path -Path $registryPath)) {

            $dir = "c:\Users"
            $ext = "pst" 
            Get-ChildItem "$dir" *$ext -r | Select-Object FullName,LastAccessTime,LastWriteTime,CreationTime,@{N='Owner';E={$_.GetAccessControl().Owner}}, @{Name="MegaBytes"; Expression={"{0:F2}" -f ($_.Length / 1MB)}}, @{N='Hostname';E={$env:computername}} | export-csv "c:\PST_Discovery.csv" -Append -NoTypeInformation 

   New-Item -Path HKLM:\Software\CC\PST_Discovery_Script -Name Already_Run –Force

}

```

[![Scheduled task](https://i.stack.imgur.com/cytQv.png)](https://i.stack.imgur.com/cytQv.png)

It works fine if I run the script manually through the Powershell console/ISE, but not through a scheduled task.

If I run it through a scheduled task, I know the script is running because it reads the registry key, and if it doesn't exist it writes a registry key, but it does't actually run the get-childitem line or export a CSV.

The scheduled task shows up on the client, and it's running using a Domain Admin credentials (me)

EDIT: Sorry, my formatting for the code went all wrong, i think it should be fixed up now

**kaspermoerch**: Yes, it's a domain admin, and thus has full permissions over the file system

**boxdog**: I actually had it writing to a UNC share, but changed it to local computer because it wasn't working. I'll try some other combinations of output location and user.

**TheIncorrigible**: Originally it was system, but it wasn't working so I edited the pushed out scheduled task and am using my domain admin account.

**Adam**:
- Yes, scheduled task is created  

- Yes, task runs script using following code: Powershell.exe -ExecutionPolicy Bypass \server1\share1\PST\_Discovery\_Script.ps1  

- Yes, it runs using my DA creds  

- Yes, the file isn't created, though it still writes the registry value 

I've checked scheduled task, see my screenshot. I'm elevating the task scheduler and manually running the task.