Sometimes we find the need to create files. We can create files with PowerShell. There are a number of different ways to create a file. We will look at some of the mechanisms here.
“” > newFile.txt
The above command creates a file by writing the empty string to the file newFile.txt.
“” | Set-Content newFile.txt
Above we use the Set-Content cmdlet to create a new file.
It is sometimes useful to create a temporary file with PowerShell. Temporary files are useful in a number of situations.
$tempFile = [System.IO.Path]::GetTempFileName()
The above code uses the static method GetTempFileName of the System.IO.Path class. We assign the name of the file to the variable $tempFile. When we are finished working with the temp file we can remove it.
1 2 3
rm $tempFile Remove-Item $tempFile del $tempFile
In this small tutorial we looked at how we can use Powershell create files.