Search notes:

PowerShell cmdLet New-Item

new-item creates a new item ain a given location. The location (or more accurately the location's provider) determine the type of the item being created.
When used in a file system, new-item is probably the closest equivalent to the touch command typically found in Unix shells.

Used in a file system provider

When used in a file system provider, new-item can be used to create files, directories and/or symbolic links (at least with NTFS).
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)"
Github repository about-PowerShell, path: /cmdlets/item/new/create-directory.ps1
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
'@
Github repository about-PowerShell, path: /cmdlets/item/new/type-file.ps1
If the file already exists, the -force parameter is required to overwrite the content of the file.

See also

The command parameter -credential.
Powershell command noun: item

Index