PowerShell: file read or write failed
Documentation: Microsoft Learn
Check file locks, directories, free space and storage connectivity when a PowerShell file operation fails.
What it means
A file or stream operation failed. File locks, unavailable storage, path problems or exhausted space can be involved, and some data may already have been written. Read the full exception and inspect the target rather than assuming a particular cause.
Likely causes
- Another process holds the file open with an exclusive lock
- Your own earlier StreamWriter or Out-File handle was never closed
- The parent directory does not exist, so the write has nowhere to land
- The path exceeds the length the underlying API will accept
- The volume filled up, or a network path dropped part way through the write
Check
Run these checks before changing anything.
What these checks establish
Test-Path and Get-Item can establish existence and metadata, but they do not prove that a later write can acquire a handle or finish. Capture the exact operation, exception and storage path.
Fix
Investigate a reported sharing violation with a handle inspection tool and close your own streams in finally or a disposal pattern. Check the parent directory, free space and storage connectivity for other I/O failures. Write a temporary file and validate it before publishing when partial output is a risk; move/replace behavior depends on the filesystem and volume boundaries.
Verify: Repeat the operation, then validate the written file’s expected size or structure and confirm the consumer can read it.