A Beginners Guide To PowerShell New-Item

Published:29 November 2022 - 4 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Today’s sponsor is n8n, the AI-native workflow automation tool built for ITOps and DevSecOps. With 100+ templates to get you started quickly and a powerful visual editor, you can automate complex workflows without giving up control. Check it out here.

 

 

 

 

 

PowerShell is a robust source shell and scripting language that houses tons of cmdlets. But as a beginner, you may first want to look into the PowerShell New-Item cmdlet. This cmdlet is one of the essential cmdlets as it allows you to create new items.

In this tutorial, you will learn how to use the New-Item cmdlet to create items, like files and directories, by running a single command.

Dive in and get started creating items in PowerShell!

Prerequisites

This tutorial comprises hands-on demonstrations. To follow along, you will need an operating system with PowerShell installed. This tutorial uses Windows 10 and PowerShell 7.

Creating Files with the PowerShell New-Item cmdlet

Perhaps you need to create new files while in PowerShell. If so, the New-Item cmdlet will do the trick. This cmdlet is handy since you do not have to switch between different windows to create items.

The basic syntax for a New-Item command is as follows where:

ParameterFunction
-PathSpecifies the location to create the item.
-NameProvides the name of the item.
-ItemTypeSets the type of item to create (i.e., a text file, PowerShell script, or even an image file.)
-ValueInputs the item’s value, such as text, string, numbers, or even other cmdlets.
New-Item -Path <path> -Name <String> -ItemType <String> -Value <Object>

To see how the New-Item works in creating files:

1. Run the below command to create a new file named file.txt with the value "powershell new-item demo file current directory." in the working directory (-Path .)

New-Item -Path . -Name "file.txt" -ItemType "file" -Value "powershell new-item demo file current directory."
Creating a file in the working directory
Creating a file in the working directory

2. Next, open File Explorer, look for and open the text file (file.txt) you created in your preferred text editor to verify the file.

PowerShell new-item : Verifying the file was created
PowerShell new-item : Verifying the file was created

3. Now, run the below command to create multiple text files (log1.txt and *log2.txt*), where commas separate the paths (c:\\logfiles).

New-Item -ItemType "file" -Path "c:\logfiles\log1.txt", "c:\logfiles\log2.txt"
Creating multiple files
Creating multiple files

💡 Separating the file paths in commas is ideal only if you create at least two or three files. If you plan to create a vast number of files, building PowerShell arrays is your best option, but it is out of the scope of this tutorial.

Creating Directories and Sub-directories

Besides creating files, the PowerShell New-Item cmdlet lets you create directories and sub-directories with a single command. This feature helps when creating a directory structure for your files.

The syntax for creating a directory is similar to creating a file, but you must specify the -ItemType as "directory".

To prepare a directory structure for your files:

1. Run the following command to create a new directory called test in the C:\\ drive.

New-Item -Path C:\ -Name "test" -ItemType "directory"
Creating a new directory
Creating a new directory

2. Next, navigate to the C:\ drive in File Explorer, look for, and verify the newly-created directory (test) exists.

Verifying the newly-created directory
Verifying the newly-created directory

3. Once verified, run the below command to create a sub-directory under the C:\reports\images

New-Item -Path "C:\reports\images" -ItemType "directory"
Creating a sub-directory
Creating a sub-directory

4. Lastly, navigate to the C:\reports directory to verify the newly-created sub-directory (images) is present.

Verifying the directory was created
Verifying the directory was created

Creating Multiple Files with a Wildcard Character

Perhaps you are looking for a way to create log files for each week of the month in one go. In that case, a wildcard character works the magic, specifically the character. The wildcard character can be helpful when you wish to create multiple files or directories with similar names.

Run the below command to create log files with the same name (logs.txt) in all (*) existing sub-directories called week1, week2, week3, and week4. The [Select-Object] cmdlet is piped to the New-Item cmdlet to print the full path of each created log file.

New-Item -Path C:\\logfiles\\* -Name logs.txt -ItemType File | Select-Object FullName
Creating files using a wildcard character
Creating files using a wildcard character

Overwriting Files with the New-Item cmdlet

When you create a new item, such as a file or directory, with the PowerShell New-Item cmdlet, you will get an error message saying the item already exists, as shown below.

Getting an error creating a file that already exists
Getting an error creating a file that already exists

But perhaps, you wish to overwrite a log file each time a scheduled task executes. If so, appending the -Force parameter lets you forcefully overwrite files without getting a prompt or error message.

1. Run the following command to create a new file named log3.txt in the c:\\logfiles directory with the content Log File 3 content.

New-Item -ItemType "file" -Path "c:\logfiles\log3.txt" -Value 'Log File 3 content.'

Next, run the Get-Content command below to get the content of the log3.txt file. Get-Content "c:\\logfiles\\log3.txt"

Get-Content "c:\logfiles\log3.txt"
Logfile 3 Content
Logfile 3 Content

3. Now, run the below command to forcefully (-Force) overwrite the existing log file (log3.txt) with new content (Log File 3 content. Hello world).

New-Item -ItemType "file" -Path "c:\logfiles\log3.txt" -Value 'Log File 3 content. Hello world' -Force
Using the -Force parameter to overwrite an existing file
Using the -Force parameter to overwrite an existing file

💡 Take caution when using the -Force parameter, as it can overwrite existing files without warning.

4. Now, run the following command to verify the content of the overwritten log file.

Get-Content "c:\logfiles\log3.txt"
Verifying modified content of the log3.txt file
Verifying modified content of the log3.txt file

Recreating a Directory

Suppose an existing directory is corrupted. Like overwriting a file, you can recreate that corrupted directory using the -Force parameter to fix it. But note that, unlike files, you cannot overwrite directories.

When you use the -Force parameter to recreate a directory, the New-Item cmdlet simply returns the object of the existing directory. The files or subdirectories inside the recreated director remain intact.

1. Run the below command to create a sub-directory called force-demo in the c:\\logfiles directory. This sub-directory is just to demonstrate the impact of recreating its parent directory.

New-Item -Path "c:\logfiles\force-demo" -ItemType Directory
Creating a sub-directory
Creating a sub-directory

2. Next, run the Get-ChildItem command below to see the contents of the c:\\logfiles directory.

Listing the contents of the c:\logfiles directory
Listing the contents of the c:\logfiles directory

3. Next, run the below command to forcefully (-Force) recreate the c:\\logfiles directory.

New-Item -Path "c:\logfiles" -ItemType Directory -Force
Recreating a directory
Recreating a directory

4. Lastly, rerun the same Get-ChildItem command below to verify the contents of the recreated directory (c:\\logfiles).

Get-ChildItem  "c:\logfiles"

As you can see, the directory is successfully recreated with its file and sub-directory intact.

Using the -Force Parameter to recreate an existing dir
Using the -Force Parameter to recreate an existing dir

Conclusion

This tutorial aims to teach you how the PowerShell New-Item cmdlet helps your workflow create files, directories, and other objects with a single command.

With the New-Item cmdlet, you can quickly create objects using its helpful parameters, and wildcard character without switching between windows.

Now, why not try creating your PowerShell profile to write instructions to execute when you launch a PowerShell session? And, of course, these instructions can include a New-Item cmdlet!

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!