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 -ItemType parameter and the value directory). The command in question will look like this:
1 |
New-Item c:\scripts\Windows PowerShell -ItemType 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:
1 |
New-Item c:\scripts\new_file.txt -ItemType file |
If the item you are trying to create already exists you’ll get back an error message similar to this:
1 |
New-Item : The file 'C:\scripts\new_file.txt' already exists. |
However, you can override the default behavior by including the -force parameter:
1 |
New-Item c:\scripts\new_file.txt -ItemType 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:
1 |
New-Item c:\scripts\new_file.txt -ItemType file -force -value "This is text added to the file" |