Compartilhar via


Using the New-Item Cmdlet

Creating a New File or Folder

New-Item is a quick and easy way to create a new file or folder on your computer. For example, suppose you want to create a new directory named Windows PowerShell within the C:\Scripts folder. To do that call New-Item along with: 1) the full path to the new folder; and, 2) the new item type (which you can specify using the -type parameter and the value directory). The command in question will look like this:

New-Item c:\scripts\Windows PowerShell -type directory

To create a new file follow the same procedure, specifying the full path name but this time setting the type to file. This command creates the file C:\Scripts\New_file.txt:

New-Item c:\scripts\new_file.txt -type file

If the item you are trying to create already exists you’ll get back an error message similar to this:

New-Item : The file 'C:\scripts\new_file.txt' already exists.

However, you can override the default behavior by including the -force parameter:

New-Item c:\scripts\new_file.txt -type file -force

If you use -force the existing file New_file.txt will be replaced by the new, empty file.

And speaking of new, empty files, you can also use the -value parameter to add some data to your new file. This command adds the phrase This is text added to the file at the same time it creates New_file.txt:

New-Item c:\scripts\new_file.txt -type file -force -value "This is text added to the file"
New-Item Aliases
  • ni