The New-TemporaryFile
cmdlet in PowerShell is used to create a temporary file easily. Creating a temp file in PowerShell is a common task when you want to store the data temporarily during the script execution.
In this article, we will discuss how to create a temp file in PowerShell using the New-TemporaryFile cmdlet.
Access the Temp Folder in PowerShell
In Windows, the operating system and application store the data temporarily in a temp folder. The system’s team folder path can be accessed using the environment variable $env:temp
$env:temp
The above PowerShell script gets the path of the current user’s temp folder as given below:
C:\Users\shelladmin\AppData\Local\Temp\
Now, let’s create a temporary file in the system temp folder.
PowerShell Create Temporary File using the New-TemporaryFile
PowerShell has a built-in cmdlet New-TemporaryFile that creates a temporary file with extension .tmp and stores it in the system’s temp folder.
$tempFile = New-TemporaryFile $tempFile
In the above PowerShell script, the New-TemporaryFile cmdlet creates a temp file `tmp913E.tmp
` and stores it in the temp folder location.
The output of the above script generates a unique file with an extension .tmp and displays file details as given below:
Create a Temporary File with a Custom Name and Extension
When working with data that needs to be stored temporarily during script execution, we need to create a temporary file with a custom name and extension. In PowerShell, it can be easily done using the following script:
# Access the temp folder path $tempFolder = $env:Temp # Define Custom Name for file $customFileName = "rabbitmq_log" # Define custom extension for file $extention = ".txt" # Join the file name with extension $customTempFileName = $customFileName + $extention # Join the system temp folder path with custom file name $customTempFilePath = Join-Path -Path $tempFolder -ChildPath $customTempFileName # Create a temp file at system temp folder $tempFile = [System.IO.FileInfo]::new($customTempFilePath) $tempFile.Create().Close() # Print the temporary file location Write-Host "Temporary file created at: $($tempFile.FullName)"
The above PowerShell script creates a temporary file with a custom name and extension `rabbitmq_log.txt
` in the system’s temp folder and print the file location as given below:
Temporary file created at: C:\Users\shellladmin\AppData\Local\Temp\rabbitmq_log.txt
Delete Windows Temporary Files
It’s important to clean up temporary files after they are no longer needed.
Use the Remove-Item cmdlet in PowerShell to remove a temporary file.
$tempFile = "C:\Users\shelladmin\AppData\Local\Temp\rabbitmq_log.txt" Remove-Item -Path $tempFile.FullName -Force
The above PowerShell script uses the Remove-Item cmdlet to remove the temporary file from the system temp folder specified by the Path parameter $tempFile. This command uses the Force parameter to forcefully delete the file if it is in use.
Conclusion
I hope the above article on how to create a temporary file in PowerShell using the New-TemporaryFile cmdlet is helpful to you.
Temporary files are useful in storing temporary data during script execution, and preprocessing.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.