powershell touch is a custom function or alias that replicates Unix touch behavior in PowerShell. It creates an empty file if missing or updates its last-write time.
function touch { param([string]$Path) if (Test-Path $Path) {(Get-Item $Path).LastWriteTime = Get-Date} else {New-Item -Path $Path -ItemType File} }
# Add to profile: notepad $PROFILE, paste function, save, reload with . $PROFILE
Usage Examples
1. Create an empty file if it does not exist
New-Item -Path .config.ini -ItemType File -Force
New-Item with -Force creates the file if missing; if it exists, it does not overwrite contents (unlike Unix touch). The -ItemType File is required in PowerShell 5.1; in newer versions, the default is assumed when an extension is present. Using -Force does not update timestamps; to mimic touch, use the custom function above.
2. Update timestamp of an existing file
function Update-FileTimestamp {
param([string]$Path)
if (Test-Path $Path) {
(Get-Item $Path).LastWriteTime = Get-Date
} else {
Write-Warning "File '$Path' not found."
}
}
Update-FileTimestamp -Path "C:Logsapp.log"
This function mirrors touch -a on Unix. It only updates the last-write timestamp without altering file content. Using Get-Item and assigning LastWriteTime is the fastest method. Avoid Set-ItemProperty which is slower.
3. Create multiple files with a loop
1..5 | ForEach-Object { New-Item -Path "file$_.txt" -ItemType File -Force }
Using ForEach-Object with a range creates five numbered empty files. -Force ensures no error if a file already exists (it will simply be left untouched). For strict touch behavior (update timestamps on existing files), wrap in a custom function that modifies LastWriteTime.
To set an alias for convenience, run: Set-Alias touch Update-FileTimestamp (or Set-Alias touch touch if you defined the function named touch).
Troubleshooting & Common Errors
| Error Message/Condition | Root Cause | Resolution |
|---|---|---|
| “Cannot find path ‘X’ because it does not exist” | Directory in path does not exist. | Create parent directory first: New-Item -Path (Split-Path $Path -Parent) -ItemType Directory -Force |
| “Access to the path ‘X’ is denied” | Insufficient permissions or file locked. | Run PowerShell as Administrator or check file handle with Get-Process | Where-Object {$_.Handle -ne 0}. Use handle.exe from Sysinternals. |
| File created with BOM (UTF-16LE) | Using echo $null > file or "" > file in PowerShell. |
Use New-Item -Path file -ItemType File or [System.IO.File]::CreateText($Path).Dispose() to avoid BOM. The latter creates UTF-8 without BOM. |
Frequently Asked Questions
What is the difference between `New-Item -ItemType File` and `Out-File` for creating an empty file?
Answer: New-Item -ItemType File creates a zero‑byte file; Out-File without input creates a file with a newline character (0x0A).
`New-Item` produces a truly empty file. `Out-File` with an empty pipeline (e.g., `”” | Out-File` or just `Out-File`) writes a BOM and newline on Windows. Use `New-Item` for exact touch behavior.
New-Item -Path "empty.txt" -ItemType File
When should I use the `-Force` flag with `New-Item` to create a file?
Answer: Use `-Force` when you want to overwrite an existing file or create missing parent directories without error.
Without `-Force`, `New-Item` fails if the target file exists or the parent path doesn’t exist. With `-Force`, it overwrites the file (like Unix `touch`’s side effect) and creates intermediate directories automatically.
New-Item -Path "./subdir/example.log" -ItemType File -Force
How do I fix ‘Access to the path is denied’ when using `New-Item`?
Answer: Run PowerShell as Administrator, change file permissions with `icacls`, or target a writable directory.
This error occurs when the process lacks write permission to the target folder or file. Verify ownership and ACLs: check `Get-Acl`, then grant write access if required. For system locations, elevate to admin.
icacls "C:ProtectedDir" /grant "Users:(OI)(CI)W"
New-Item -Path "C:ProtectedDirtest.file" -ItemType File
Does `New-Item -ItemType File` work on PowerShell Core (Cross-platform)?
Answer: Yes, `New-Item -ItemType File` works identically on Windows, Linux, and macOS in PowerShell 7+.
PowerShell Core (6.x and 7.x) is fully cross‑platform. The cmdlet creates a zero‑byte file on all OSes. Unix‑style symbolic links (`-ItemType SymbolicLink`) also work natively.
# Works on Linux/macOS
New-Item -Path "~/touch_test.txt" -ItemType File
What is the fastest way to create an empty file in PowerShell?
Answer: Use `New-Item -ItemType File` or the alias `ni`; both are ~5ms per file and require no pipeline overhead.
Avoid `Out-File` or `Set-Content` – they add I/O overhead. For bulk operations, pipe to `New-Item` using the `-Path` parameter directly. Minimal syntax:
ni test.txt
# Or for multiple files
'f1.txt','f2.txt' | ni -ItemType File

Command Line Expert & Software Engineer
Welcome! I’m Thomas Heinrich, a software engineer and system administrator with a deep passion for the Command Line Interface (CLI). With years of experience navigating the terminal, building backend architectures, and automating server deployments, I created this space to share practical, real-world terminal knowledge.
Whether you are a beginner taking your first steps in a Linux environment or a seasoned DevOps engineer looking to optimize your deployment scripts, you will find actionable solutions here. My goal is to help you ditch the mouse, speed up your workflow, and harness the full power of the command line.