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.

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 File or Folder.
  • NAME is the name of the item.
  • ITEMTYPE specifies the newly created item like “file”, “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 names myfile.txt in the current working directory.

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.

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.

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

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

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

Create New Profile

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

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 provided inside double quotes and separated from the commas.

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 provided inside double quotes and separated from the commas.

New-Item -ItemType "directory" -Path "c:\test", "c:\Logs"
READ  nbtstat Command Tutorial

Leave a Comment