PowerShell Guide: Creating Files and Folders

PowerShell Script img

While the Windows graphical user interface (GUI) makes creating files and folders a simple task, PowerShell offers an efficient alternative for scripting and administrative needs. In this guide, we’ll explore essential commands in your PowerShell Terminal to streamline the process.

PowerShell: Create a New Folder

Creating a new folder is a breeze with the New-Item command. Specify the ItemType as Directory and Name of the new folder within the cmdlet as shown.

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

PowerShell: Create a New File

Similar to creating a folder, the New-Item command is your go-to tool. This time, set the ItemType as File to generate a new file.

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

Tip: For added functionality, explore using Add-Content and Get-Content to manage file content.

PowerShell: Create New Folder (if not exists)

Opting for a script approach, this example creates a folder only if it doesn’t exist. The New-Item command runs conditionally based on the true/false result of Test-Path.

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

Tip: To enhance efficiency, the -Force flag ensures seamless creation without unnecessary prompts.

Effortlessly incorporate these PowerShell commands into your workflow to enhance your file and folder creation process. Whether you’re scripting or managing administrative tasks, PowerShell provides the flexibility you need.


Leave a Reply

Your email address will not be published. Required fields are marked *

Popular Posts

Blog Categories