Used in a file system provider
PS C:\users\rene> new-item emptyFile.txt
PS C:\users\rene> new-item someDirectory -itemType directory
PS C:\users\rene> new-item someDirectory\link-to-file -target emptyFile.txt -itemType symbolicLink
Note that administrative privileges are required to create a symbolic link.
Create a (file system) directory
In order to create a directory, the -type
flag must be given the value directory
:
$dirName = 'a-folder'
if (test-path $dirName) {
write-host "Directory $dirName already exists, going to remove it"
remove-item -recurse -force $dirName
}
$createdDir = new-item -type directory a-folder
write-host "Directory was created at $('{0:yyyy-MM-dd HH:mm:ss}' -f $createdDir.creationTime)"
When a directory is created, PowerShell will create all necessary directories (such as the
shell command
mkdir -p
):
remove-item -recurse -force $env:temp\dir -errorAction ignore
new-item $env:temp\dir\sub-dir\sub-sub-dir -type directory
Note: there is also a mkdir
function that comes with a PowerShell installation.
Create a file
By specifying the -type
parameter with the value file
, new-item
creates a file:
new-item -type file xyz.txt -value @'
some text
that goes into
the file named xyz.txt
'@
If the file already exists, the -force
parameter is required to overwrite the content of the file.