Create a new item in a namespace. Create files and folders, Symbolic Links, registry keys and registry entries.

      New-Item [-name] string [-path string ]
         [-force] [-credential PSCredential] [-itemType string]
            [-value Object] [-whatIf] [-confirm]
               [-UseTransaction] [CommonParameters]

   -name string
       The name of the new item.

   -path string
       The path(s) to the items. Wildcards are permitted.
       Use a dot (.) to specify the current location. 

   -value Object
       The value the new item, can be piped.

       Override restrictions that prevent the command from succeeding, apart
       from security settings. e.g. rename an existing file.
       Create a file when the directories in the path do not
       exist (PowerShell will create them)

   -itemType string
       The provider-specified type of the new item
       For a FileSystem location the accepted values are:
          file, directory, SymbolicLink, Junction, HardLink
       For a Certificate drive the accepted values are:
          Certificate Provider, Certificate, Store, StoreLocation

       This is a dynamic parameter made available by the Alias provider.
       For more information, see New-Alias. PowerShell 7.3+

   -Type string
       An alias for -itemType above

       Describe what would happen if you executed the command without 
       actually executing the command.
       Prompt for confirmation before executing the command.

   -credential PSCredential
       Use a credential to validate access to the file. Credential represents
       a user-name, such as "User64" or "Domain64\User64", or a PSCredential
       object, such as the one retrieved by using the Get-Credential cmdlet.
       If you type a user name, you will be prompted for a password.
       This parameter is not supported by any PowerShell core cmdlets or providers.

       Include the command in the active transaction.

Standard Aliases for New-Item: md, ni
mkdir is a function that calls New-Item

New-Item creates a new item and sets its value. The types of items that can be created depend upon the location of the item. For example, in the file system, New-Item is used to create files and folders. In the registry, New-Item creates registry keys and entries.

New-Item can also set the value of the items that it creates. For example, when creating a new file, New-Item can add initial content to the file.

Under Windows and in PowerShell Core edition, New-Item -Type File -Value will create BOM-less UTF-8 files by default.
Files in this format will be misinterpreted by Get-Content as being ASCII.
This file encoding can be configured by setting the $PSDefaultParameterValues preference variable.

By default new-item will display the item just created, in fact it writes to the pipeline (which by default is passed to the host). This can be suppressed by assigning to a dummy variable ($null = new-item...) or just redirecting the pipeline to out-null.

Files created with new-item will be 0 Bytes in size, larger files, filled with NUL characters can be created by calling .net [System.IO.File]::Create(file).
This will create a 10 MiB ANSI file:

$file = 'C:\demo\dummy.txt'
$sizeBytes = 10MB
$file = [System.IO.File]::Create($file)

.Net also provides a way to generate 0 byte temporary files: [System.IO.Path]::GetTempFileName()


Create a text file:

PS C:\> new-item -path C:\docs -name SS64.txt -type "file" -value "some text"

Note that this won’t overwrite an existing file; to overwrite an existing file, use set-content instead of new-item.

Create a directory named 'Demo Folder' in the C: drive:

PS C:\> new-item -path c:\ -name "Demo Folder" -type directory

Create a new Temporary file:

PS C:\> $tmp = [System.IO.Path]::GetTempFileName()
PS C:\> get-Item -path $tmp

Create a new variable called $myVar:

PS C:\> new-item variable:\myVar -value "testing123"

Create a Symbolic Link to Windows File Explorer:

PS C:\> New-Item -Path "$env:USERPROFILE\desktop\test.lnk" -Value C:\WINDOWS\explorer.exe -ItemType SymbolicLink

Create a new Function:

PS C:\> $myMultiLineFunc = { #add the function definition here }
PS C:\> New-Item -Path function:demo-func -Value $myMultiLineFunc

If a folder does not exist, then create it:
if ( -Not (Test-Path "F:\BACKUP"))
 New-Item -Path "F:\BACKUP" -ItemType Directory | out-null

Create a PowerShell profile in the path specified by the $profile variable:

PS C:\> new-item -path $profile -type file -force

$profile is an automatic (built-in) variable that stores the path and file name of your PowerShell profile

By default, the profile file does not exist, even though PowerShell has a filename for it, the file can be created using New-Item or notepad.

After using the command above to create a profile, you can enter aliases, functions, and scripts in the profile to customize your shell.

“Selection is creation” ~ Koichi (Japanese Interior Designer)

Related PowerShell Cmdlets

Clear-item - Remove content from a variable or an alias.
Copy-item - Copy an item from a namespace location.
Get-item - Return an object that represents an item in a namespace.
Invoke-item - Invoke an executable or open a file (START).
Move-item - Move an item from one location to another.
New-Object - Create a new .Net object.
Set-item - Set the value of a provider pathname.
Remove-item - Remove an item.
Rename-item - Change the name of an existing item.
Help about_automatic_variables.

Copyright © 1999-2024
Some rights reserved