PowerShell Create Files and Folders with New-Item

PowerShell Create Files and Folders with New-Item

PowerShell Tips Editor 5 min read
PowerShell Create Files and Folders with New-Item

Before your script can write a log, copy a config, or store output, the target directory and file need to exist. PowerShell create file operations are handled by New-Item — a single cmdlet that creates files, directories, symbolic links, and registry keys. This guide covers every scenario: creating individual files and folders, building nested directory structures in one command, writing initial content, creating symlinks, and using -Force to overwrite safely.

Create a New File with New-Item

Use the -ItemType File flag to create a new empty file:

# Create an empty text file
New-Item -Path 'C:\Logs\app.log' -ItemType File

# Create in the current directory
New-Item -Name 'settings.json' -ItemType File

# Create and display the resulting object
$newFile = New-Item -Path 'C:\Temp\output.txt' -ItemType File
Write-Output "Created: $($newFile.FullName)"
    Directory: C:\Logs

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/4/2026  9:15 AM               0 app.log

Created: C:\Temp\output.txt

New-Item returns the newly created item as an object. The file is created empty (0 bytes). If the parent directory doesn’t exist, the command fails unless you add -Force.

Create a New Folder

Use -ItemType Directory to create a new directory:

# Create a directory
New-Item -Path 'C:\Logs\Archive' -ItemType Directory

# Create with a variable path
$today = Get-Date -Format 'yyyy-MM-dd'
New-Item -Path "C:\Logs\Archive\$today" -ItemType Directory | Out-Null

# Suppress output with | Out-Null (common in scripts)
New-Item -Path 'C:\Backup\Daily' -ItemType Directory | Out-Null
    Directory: C:\Logs

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           5/4/2026  9:15 AM                Archive

Create Nested Folders at Once

Use -Force to create an entire directory hierarchy in one command, even if some levels already exist:

# Create multiple nested levels at once
New-Item -Path 'C:\Projects\WebApp\Logs\Archive\2026' -ItemType Directory -Force | Out-Null

# Verify the structure
Get-ChildItem 'C:\Projects\WebApp' -Recurse -Directory | Select-Object FullName
FullName
--------
C:\Projects\WebApp\Logs
C:\Projects\WebApp\Logs\Archive
C:\Projects\WebApp\Logs\Archive\2026

Without -Force, trying to create a path where any intermediate directory doesn’t exist throws an error. With -Force, PowerShell creates all missing levels. If the directory already exists, -Force does nothing — it doesn’t delete or overwrite existing folders.

Creating with Initial Content

The -Value parameter writes content to the file at creation time:

# Create a file with initial text content
New-Item -Path 'C:\Config\settings.ini' -ItemType File -Value "[General]`nLogLevel=Info`nMaxRetries=3"

# Create a JSON config file
$defaultConfig = @{
    Server   = 'localhost'
    Port     = 8080
    Debug    = $false
} | ConvertTo-Json

New-Item -Path 'C:\Config\appsettings.json' -ItemType File -Value $defaultConfig

# Create a placeholder script
New-Item -Path 'C:\Scripts\deploy.ps1' -ItemType File -Value "# Deployment script`n# Created: $(Get-Date -Format 'yyyy-MM-dd')`n"
    Directory: C:\Config

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           5/4/2026  9:15 AM             276 appsettings.json

For more complex file writing, use Set-Content or Out-File after creating the file. The -Value parameter is convenient for simple initial content.

Creating Symbolic Links

Create file or directory symbolic links (requires admin on Windows):

# Create a symbolic link to a file
New-Item -ItemType SymbolicLink -Path 'C:\Logs\current.log' -Target 'C:\Logs\app-2026-05-04.log'

# Create a symbolic link to a directory
New-Item -ItemType SymbolicLink -Path 'C:\App\config' -Target 'C:\Config\Production'

# Create a hard link (same drive only, files only)
New-Item -ItemType HardLink -Path 'C:\Backup\app.log' -Target 'C:\Logs\app.log'

Symbolic links require elevation on Windows by default. Hard links don’t require elevation but only work on the same volume. Junction points (directory links on Windows) use -ItemType Junction.

Using -Force to Overwrite

-Force creates the item even if it already exists or if parent directories are missing:

# Create file — error if exists without -Force
# New-Item -Path 'C:\Logs\app.log' -ItemType File  # Throws if exists

# Overwrite if exists (replaces content with empty file)
New-Item -Path 'C:\Logs\app.log' -ItemType File -Force

# Create directory hierarchy regardless of what exists
New-Item -Path 'C:\Deep\Nested\Path\To\Dir' -ItemType Directory -Force | Out-Null

# Idempotent file creation — safe to run multiple times
function Ensure-File {
    param([string]$Path, [string]$DefaultContent = '')
    if (-not (Test-Path $Path -PathType Leaf)) {
        $dir = Split-Path $Path -Parent
        New-Item -Path $dir -ItemType Directory -Force | Out-Null
        New-Item -Path $Path -ItemType File -Value $DefaultContent | Out-Null
        Write-Output "Created: $Path"
    }
}

Common Errors and Fixes

  • Item already exists error — use -Force: New-Item throws “An item with the specified name already exists” when the target path exists. Add -Force to overwrite files, or use Test-Path to check first and skip creation if the item already exists.
  • Parent directory must exist unless -Force is used: Creating C:\New\Deep\file.txt when C:\New\Deep\ doesn’t exist fails without -Force. Either create the parent directory first with New-Item -ItemType Directory -Force, or add -Force directly to the file creation command.

Related Cmdlets / See Also

Wrapping Up

New-Item creates files, folders, symlinks, and more with a single consistent syntax. Use -Force for both nested directory creation and overwriting. Add | Out-Null to suppress the returned object when you don’t need it. For idempotent scripts, combine Test-Path with New-Item -Force to create only what’s missing. Your next step: write the directory setup section of your next automation script using New-Item -Force to ensure all required folders exist.

Send-Item -To