Menu & Search

How to Create New Files & Folders in PowerShell

How to Create New Files & Folders in PowerShell

This is a post on how to create new files and folders using PowerShell.

Creating new files and folders in Windows is generally done via GUI. That’s what it’s there for, the simplicity. But when you’re scripting or doing admin work, you might want to create new files and folders via command.

This guide covers performing the following in your PowerShell Terminal –

# PowerShell: Create a New Folder
# PowerShell: Create a New File
# PowerShell: Create New Folder (if not exists)

Create New Folder in Powershell

New-Item is the command to create the new folder and item. We just need to amend the ItemType to be a Directory for us to create a folder.

# create new folder in powershell
New-Item -ItemType Directory -Name Test_Stuff
PowerShell New-Item Directory

Create New File in PowerShell

As mentioned above, it’s the same command, New-Item, and we’re changing the ItemType to File this time.

# create new file in powershell
New-Item -ItemType File -Name Test_File.txt
PowerShell New-Item File

Further reading: We also can add text to this file by using Add-Content & Get-Content (to view content).

Create New Folder (if not exists) in PowerShell

This time, it’s a script rather than a one-liner/cmdlet.

We’re only creating a new folder only if it doesn’t already exist – “C:\temp\demo”

When we run this script it also creates the C:\temp directory if it doesn’t already exist.

The create folder command is New-Item, which runs conditionally depending on the Test-Path true/false result.

# create folder if not exists
$path = "c:\temp\demo"
If(!(Test-Path $path) ){
    New-Item -ItemType Directory -Force -Path $path
}
PowerShell Create Folder if not exists

2 Comments

  1. […] post is one part of my other post – How to Create New Files & Folders in PowerShellThe linked post covers more info on creating files vs […]

  2. […] have a similar post on How to Create Files & Folders in PowerShell if of interest, and more random tips can be found in the PowerShell Tips […]