Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Facebook Twitter Instagram
Tech News Today
  • Hardware
    • Motherboards
    • CPUs
    • Graphic Cards
    • RAM
    • SSDs
    • Computer Cases
    • Monitors
    • Peripherals
    • Power Supply Unit
    • PC Builds
    • Computer Tips
  • Software
  • Operating System
    • Windows
    • Mac
    • Linux
  • Gaming
  • Mobile
  • Console
  • More
    • Internet
    • Networking
    • Security
    • Buyer’s Guide
    • Gadgets
    • Laptops
    • Reviews
    • How To
    • News
Tech News Today
Home»Windows»How to Create File in PowerShell Command

How to Create File in PowerShell Command

Abhishek SilwalBy Abhishek SilwalDecember 10, 2022
how-to-create-file-in-powershell-command

Creating a file using graphical tools like File Explorer is extremely easy in Windows. However, you don’t have a lot of options and configurations you can set while doing so. Additionally, you may need to create multiple files at once or create similar files in multiple directories.

In such cases, using command line tools like PowerShell is the most effective method. You can also include the PowerShell cmdlets to create files in a PowerShell script to perform flexible and advanced file-handling tasks.

Table of Contents

  • How to Create File in PowerShell Command?
    • Creating New File
    • Creating Multiple Files
    • Creating File After Checking if It Exists
    • Creating File by Overwriting Pre-existent File
    • Creating Files in Multiple Directories
    • Creating Symbolic Link File

How to Create File in PowerShell Command?

The default cmdlet to create a file in PowerShell is the New-Item cmdlet. You can also use other cmdlets, such as Out-File, by using them in certain manners. However, they have some restrictions, and you can’t use them in all scenarios.

So, we will only be discussing how you can use the New-Item cmdlet to create files for different purposes in this article. You can also use the alias ni instead of New-Item

Creating New File

On PowerShell, enter the command New-Item -Path “File full path” -ItemType File to create the file. For example, New-Item -Path “D:\New\test.txt” -ItemType File

new-item-file

Alternatively, you can assign the file path to a variable and use the variable in the -Path attribute. It is especially useful if you need to use this path multiple times in a script. For instance,

$FilePath = “D:\New\test.txt”
New-Item -Path $FilePath -ItemType File

You can create a file with any extension, not just .txt

You can also directly input some value or text into the file while creating it by using the -Value attribute. For example, New-Item -Path “D:\New\test.txt” -ItemType File -Value “Test File”

Creating Multiple Files

You can use multiple file paths in the -Path attribute separated by commas to create them simultaneously. For instance, New-Item -Path "D:\New\test.txt", "D:\New\test1.csv" creates two files test.txt and test1.csv inside the D:\New\ folder

new-item-multiple-files

Creating File After Checking if It Exists

If a file is already present in the folder path, you can’t create a new file there. Additionally, trying to do so will create an exception and crash the script. So, if you need to create a file through a script, it’s always to check if a file already exists before writing the cmdlet to create it.

You can use the Test-Path cmdlet to do so in the following way:

if (-not (Test-Path -Path “File full path”)){
   New-Item -Path “File full path” -ItemType File
}
else{
   //anything you want to do if the file already exists
}
new-item-test-path-if

And the same as for creating a new file, you can assign the path to a variable and use the variable in both instances instead.

You can also catch this exception by using Try and Catch statements.

Creating File by Overwriting Pre-existent File

You can also forcibly overwrite a file if it is already present in order to avoid the exception error. You need to use the -Force attribute to do so. The syntax is:
New-Item -Path “Full file path” -ItemType File -Force

new-item-overwrite

Creating Files in Multiple Directories

It is also possible to create files in multiple directories at the same time by including all the directories inside the -Path attribute.

For instance, New-Item -Path "D:\New", "D:\New Folder", "D:\New folder (2)" -Name temp.txt -ItemType File creates temp.txt inside New, New Folder, and New Folder (2) folders in the D: root directory.

new-item-file-multiple-directory

You can also use wildcards to specify multiple folders. For example, New-Item -Path D:\* -Name temp.txt -ItemType File creates temp.txt in all files directly inside the D: drive.

Creating Symbolic Link File

You can use the SymbolicLink item type with this cmdlet to create a symbolic link pointing to a file.

For example, New-Item -ItemType SymbolicLink -Path "D:\New Folder\link" -Target "D:\New\temp.txt" creates a symbolic link of D:\New\temp.txt inside D:\New Folder with the name link.

new-item-symbolic-link

It is also possible to create a hard link by using the -ItemType HardLink attribute.

how-to
Abhishek Silwal
  • LinkedIn

Abhishek Silwal is an Electronics Engineer and a technical writer at TechNewsToday. He specializes in troubleshooting a wide range of computer-related issues. His educational background in Electronics Engineering has given him a solid foundation in understanding of computers. He is also proficient in several programming languages and has worked on various robotics projects. Even in his early days, he used to tinker with various computer components, both hardware, and software, to satiate his curiosity. This experience has given him a breadth of experience that goes beyond his educational qualification. Abhishek has been writing articles on dealing with varieties of technical issues and performing specific tasks, especially on a Windows machine. He strives to create comprehensive guides on fixing many system and hardware issues and help others solve their problems. You can contact him at abhisheksilwal@technewtoday.com

Related Posts

Power on Self test

What is Power on Self Test (POST)? How Does It Work

February 6, 2023
how to make cursor bigger

How to Make Cursor Bigger

February 3, 2023
enable secure boot msi bios

How to Enable Secure Boot on MSI Motherboard

February 3, 2023
how to add device to microsoft account

How to Add Device to Microsoft Account

February 2, 2023
how to delete win backup files

How to Delete Backup Files on Windows

January 29, 2023
how to reset password on hp laptop

How to Reset Password on HP Laptop

January 30, 2023
Add A Comment

Leave A Reply Cancel Reply

Latest Posts
Power on Self test

What is Power on Self Test (POST)? How Does It Work

February 6, 2023
usb a vs usb c

USB A Vs USB C – What’s the Difference?

January 31, 2023
how long does a cmos battery last

How Long Does a CMOS Battery Last

January 25, 2023
You may also like
find cursor windows

How to Quickly Find Cursor in Windows

February 6, 2023
Surface Mouse Not Working

Surface Mouse Not Working? Try These Fixes

February 6, 2023
how-to-change-ink-in-hp-printer

How to Change Ink in HP Printer? Step-by-Step Guide

February 6, 2023
Recommended
Cookie Clicker Garden Guide

Cookie Clicker Garden Guide to Unlocking Every Seed

September 26, 2021
monitor no signal

Computer Turns On But Monitor Says No Signal (9 Ways To Fix)

November 10, 2022
Facebook Twitter Pinterest
  • Home
  • About Us
  • Our Team
  • Editorial Guidelines
  • Privacy Policy
  • Affiliate Disclosure
© 2023 TechNewsToday.

Type above and press Enter to search. Press Esc to cancel.