PowerShell New-Item Tutorial

The New-Item is a PowerShell command used to create new items for different cases. New-Item command can be used to create a file or folder. Also, the New-Item can be used to set or add values to the items. Another case of the New-Item usage is the creation of registry values.

New-Item Command Syntax

The New-Item command syntax is like below.

New-Item -Path PATH -Name NAME -ItemType ITEMTYPE -Value VALUE
  • PATH is the path of the item if the ITEMTYPE is a File or Folder.
  • NAME is the name of the item.
  • ITEM_TYPE specifies the newly created item like “file”, and “directory”.
  • VALUE is the content or value of the newly created item.

Create New File

The New-Item command can be used to create a new file. In order to create a new file, only the -Name and -ItemType properties are required. But -Path and -Value properties are used generally for extra configuration. In the following example, we will create a file name myfile.txt in the current working directory. But we do not put any value inside the newly created file.

PS> New-Item -Name "myfile.txt" -ItemType "file"

In the following example with the New-Item to create a file, we will also specify the -Path of the file as D:\Backup and provides some text content with the -Value property.

PS> New-Item -Path "D:\Backup" -Name "myfile.txt" -ItemType "file" -Value "This is a text string."

Create New Directory

The New-Item command can be also used to create a folder or directory. The -ItemType should be specified as “directory”. Also, the -Name should be specified too. If the -Path is not specified the new directory will be created in the current working directory.

PS> New-Item  -Name "Backup" -ItemType "directory"

In the following example, we will specify the -Path property for the newly-created directory.

PS> New-Item -Path "d:\" -Name "Backup" -ItemType "directory"

Create New Profile

PowerShell is an advanced scripting language that has the ability to create different sessions and different profiles. Profiles are used to change and customize the PowerShell. The profile is stored inside the $profile variable of the PowerShell and a new profile can be created by setting -ItemType as “file”.

PS> New-Item -Path $profile -ItemType "file" -Force

Create Multiple Files

Multiple directories can be also created with the New-Item. Multiple files will be specified for the -Path property. The multiple files are provided inside double quotes and separated from the commas.

PS> New-Item -ItemType "file" -Path "c:\test.txt", "c:\Logs\test.log"

Create Multiple Directories

Similar to creating multiple files multiple directories can be created with the New-Item command. Multiple directories will be specified for the -Path property. The multiple directories are provided inside double quotes and separated from the commas.

PS> New-Item -ItemType "directory" -Path "c:\test", "c:\Logs"
See also  How To Enable Hibernate For Windows?

Leave a Comment