The New-Item command is able to create files, directories, and keys:
New-Item $env:Temp ewfile.txt -ItemType File New-Item $env:Temp ewdirectory -ItemType Directory New-Item HKLM:SoftwareNewKey -ItemType Key
When creating a file using New-Item in PowerShell, the file is empty (0 bytes).
In PowerShell 5, New-Item gained the ability to create symbolic links, junctions, and hard links:
- A symbolic link is a link to another file or directory. Creating a symbolic link requires administrator privileges (run as administrator)
- A hard link is a link to another file on the same drive
- A junction is a link to another directory on any local drive. Creating a junction does not require administrative privileges
The links may be created as follows:
New-Item LinkName -ItemType SymbolicLink -Value \ServerShare New-Item LinkName.txt -ItemType HardLink -Value OriginalName.txt New-Item LinkName -ItemType Junction -Value C:Temp
Temporary files:
If a script needs a file to temporarily store data, the New-TemporaryFile command may be used:
New-TemporaryFile
This command was introduced with PowerShell 5. The earlier versions of PowerShell may use the Path.GetTempFileName static method:
[System.IO.Path]::GetTempFileName()
Both commands create an empty file. The resulting file may be used with Set-Content, Out-File, or any of the commands that write data to a file.
If a script needs a file to temporarily store data, the New-TemporaryFile command may be used:
New-TemporaryFile
This command was introduced with PowerShell 5. The earlier versions of PowerShell may use the Path.GetTempFileName static method:
[System.IO.Path]::GetTempFileName()
Both commands create an empty file. The resulting file may be used with Set-Content, Out-File, or any of the commands that write data to a file.
The Remove-Item command may be used to remove an existing item under a provider; for example:
$file = [System.IO.Path]::GetTempFileName() Set-Content -Path $file -Value 'Temporary: 10' Remove-Item $file
Providers such as filesystem and registry are reasonably flexible about removing items. When removing a directory or key with children, the recurse parameter should be used.
The certificate provider restricts the use of Remove-Item to certificates; certificate stores cannot be removed.