Create Temp Directory in PowerShell

Create temp directory in PowerShell

Using [System.IO.Path]::GetTempPath() Method with New-Item cmdlet

Use the [System.IO.Path]::GetTempPath() with New-Item cmdlet to create a temporary directory in PowerShell.

First, we used the GetTempPath() method of the System.IO.Path class to get the path of a current user’s temporary folder. This method returned a string type value, which we stored in the $temporaryPath variable. You can chain the GetType() method as $temporaryPath.GetType() to see its data type. The GetTempPath() method produces the SecurityException if the caller would not have the necessary permissions.

Next, we used the New-Item cmdlet with various parameters to create a directory in the $temporaryPath. Note that the New-Item cmdlet will only create a specified directory if it does not exist; otherwise, it will display an error saying an item with the specified name XXX already exists.

The New-Item is used to create a new item and set its value. Here, the specified item type differs based on the item’s location. For instance, New-Item creates files/folders in the file system; in the registry, it creates keys and entries. This cmdlet is also used to set the value of the newly created item; for example, we can add initial data to a file that we created using New-Item.

The -ItemType parameter was used to specify the new item’s provider-specified type. We can use different values for this parameter based on the current provider. For instance, if we are in the FileSystem drive now, we can use the following values for the -ItemType parameter (we used Directory in the above examples):

  • File
  • Directory
  • SymbolicLink
  • Junction
  • HardLink

The -Path parameter was used to specify the path of the new item’s location; when this parameter is omitted, the default will be the current location represented with a dot (.).

Now, if you want to create a temporary file in the temporary directory, you can do that using the following command.

We used the GetTempFileName() method of the System.IO.Path class to create a zero-byte uniquely named temporary file on disk and return the complete path of the created file. This returned value would be of string data type. This method can generate IOException if it cannot create the temporary file or the I/O error occurs; for instance, if no unique temporary file name is available.

Using System.GUID Class

This solution is useful when we are not concerned with the directory names to avoid the Directory Already Exists error. Whenever you run the following code, you create a directory with a unique name in the system’s temporary folder using PowerShell. To do that:

  • Use the GetTempPath() method to get the path of the system’s temporary folder.
  • Use NewGuid() to create a unique identifier.
  • Use New-Item to create a directory.

This code fence created a new directory with a unique name in the system’s temporary folder. The [System.IO.Path]::GetTempPath() assigned the system’s temporary folder path to the variable $parent using the GetTempPath() method of the System.IO.Path class.

The [System.Guid]::NewGuid() created a new unique identifier and assigned it to the variable $name using the NewGuid() method of the System.Guid class. Next, the New-Item (we learned in the first sections) created a new directory in the specified parent directory using the New-Item cmdlet.

That’s all about how to create temp directory in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *