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.

Create a function

new-item can be used to create a function with a dynamic name:
$funcName = 'hello-world'
new-item function:$funcName -value {write-host 'Hello world'}

See also

The command parameter -credential.
Powershell command noun: item

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1759562216, '216.73.216.149', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/command-inventory/noun/item/new/index(98): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78