How to Use PowerShell new-item to Create Files and Directories?

Recently, while working on a project for a client in San Francisco, I needed to automate the creation of multiple directories and files for a large-scale data migration. We can do this by using the PowerShell new-item cmdlet. In this tutorial, I will explain how to use the New-Item cmdlet in PowerShell to create files and directories with examples.

To use the PowerShell New-Item cmdlet, specify the -Path parameter with the target directory, the -Name parameter with the desired item name, and the -ItemType parameter to define the type of item (e.g., file or directory). For example, to create a new text file named ProjectPlan.txt in the C:\Projects\Dallas directory, use: New-Item -Path "C:\Projects\Dallas" -Name "ProjectPlan.txt" -ItemType "file". This command creates the specified item in the desired location.

PowerShell new-item

The New-Item cmdlet is part of the Microsoft.PowerShell.Management module and is used to create new items and set their values. The types of items you can create depend on the location specified. For example, you can create files and directories in the file system, registry keys in the registry, and so on.

Syntax

Here is the syntax of the PowerShell new-item cmdlet:

New-Item [-Path] <string> [-Name] <string> [-ItemType] <string> [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [<CommonParameters>]

Parameters:

  • -Path: Specifies the path where the new item will be created.
  • -Name: Specifies the name of the new item.
  • -ItemType: Specifies the type of item to create (e.g., file, directory, registry key).
  • -Value: Sets the value of the new item.
  • -Force: Forces the cmdlet to create the item even if it exists.
  • -Credential: Specifies the user account that has permission to perform this action.
  • -WhatIf: Shows what would happen if the cmdlet runs.
  • -Confirm: Prompts for confirmation before running the cmdlet.

Check out PowerShell Copy-Item

PowerShell new-item Examples

Now, let me show you how to use the new-item PowerShell cmdlet with some real examples.

1. Create Files and Directories using the New-Item cmdlet

First, let me show you how to create files and directories using the PowerShell New-Item cmdlet with examples.

Create a File

To create a new file in a specific directory, use the following New-Item command:

New-Item -Path "C:\MyNewFolder" -Name "NewDocument.txt" -ItemType "File"

This command creates a new file named “NewDocument.txt” in the “MyNewFolder” folder.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

PowerShell new-item

Create a Directory

To create a new directory, use the New-Item cmdlet like the below PowerShell script.

New-Item -Path "C:\NewFolder" -Name "NewFolder" -ItemType "Directory"

This command creates a new ” NewFolder ” directory in the C drive.

Here is another example.

Suppose you are working on a project for a client in New York. You can create a directory structure to organize your project files:

New-Item -Path "C:\Projects\NYClient" -Name "ProjectFiles" -ItemType "Directory"

Here is also another real example.

Suppose you want to create a log file for recording activities in the Chicago office. Use the PowerShell script below.

New-Item -Path "C:\Logs\ChicagoOffice" -Name "ActivityLog.txt" -ItemType "File"

Check out PowerShell Write-Output

2. Create Registry Keys

Creating registry keys can be crucial for system configuration and management. Here’s how to create a new registry key:

New-Item -Path "HKCU:\Software\MyCompany" -Name "Settings" -ItemType "Key"

This command creates a new registry key named “Settings” under “HKCU:\Software\MyCompany”.

Let me show you another example.

Suppose you want to set up a registry key for a San Francisco branch.

To configure settings specific to the San Francisco branch, you might create a registry key by using the PowerShell script below.

New-Item -Path "HKLM:\Software\Company\SanFrancisco" -Name "BranchSettings" -ItemType "Key"

3. Set Values with the New-Item cmdlet

You can also set the value of the new item using the -Value parameter. For example, to create a file and set its content:

New-Item -Path "C:\MyNewFolder" -Name "NewDocument.txt" -ItemType "File" -Value "Hello, PowerShell!"

This command creates a new file named “NewDocument.txt” and writes “Hello, PowerShell!” into it.

Here is the exact output in the screenshot below:

PowerShell new-item examples

Read PowerShell Write-Host

4. Using the -Force Parameter

The -Force parameter of the New-Item cmdlet is useful when you need to create an item that already exists or when you need to create a hidden or read-only item. For example:

New-Item -Path "C:\MyNewFolder" -Name "ExistingDocument.txt" -ItemType "File" -Force

This command forces the creation of “ExistingDocument.txt” even if it already exists.

Conclusion

I hope you now understand how to use the PowerShell New-Item cmdlet with these useful examples. The New-Item cmdlet in PowerShell is used to create various items such as files, directories, and registry keys. I executed all the PowerShell scripts in my local system, and everything is working fine. Let me know in the comment below if you face any errors.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.