Powershell Keeps Resetting To Default Temp Folder

371 Views Asked by At

I am running a PowerShell script that extracts data from a database. However, I do not have enough space on the default %Appdata%Local/Temp/ folder. I know this because I get the error:

Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect key file for table 'C:\Windows\SERVIC~2\NETWOR~1\AppData\Local\Temp#sql7ec_3_3.MYI'; try to repair it"

To fix this issue I am trying to change the Temp folder for PowerShell. I have changed it in regedit to:

TMP   D:\Temp
Temp  D:\Temp

In addition I run this code at the start of my script:

# Set Environmental Variables
[Environment]::SetEnvironmentVariable("TEMP", "D:\Temp\")
[Environment]::SetEnvironmentVariable("Temp", "D:\Temp\")
[Environment]::SetEnvironmentVariable("TMP", "D:\Temp\")

# Navigate to these temp folders
$TempDir = [System.IO.Path]::GetTempPath()
cd $TempDir # This actually returns 'D:\Temp\'

However, when the script runs I still get the error message:

Exception calling "ExecuteNonQuery" with "0" argument(s): "Incorrect key file for table 'C:\Windows\SERVIC~2\NETWOR~1\AppData\Local\Temp#sql7ec_3_3.MYI'; try to repair it"

Any advice on why it is defaulting to the %Appdata% folder and how to change it?

1

There are 1 best solutions below

0
On

I found the solution, and it was to do with the Environmental Variables. I correctly changed the TEMP environment for PowerShell, however, since I called a DB it was the DB doing the actual calculation and using its own TEMP folder separate from PowerShell. When I changed the TEMP file location for the MySQL database it worked perfectly.