Out-file -force, no directories created

My code is:

$path = "c:\no-such-dir\00.txt" "foo" | Out-File -force -filePath $path 

Mistake:

Out-File: could not find part of path 'C: \ no-such-dir \ 00.txt'

help out-file -full

For example, Force will override the read-only attribute or create directories to complete the file path, but it will not attempt to modify the permission file.

So it seems that he should create a "no-such-dir", but it is not. What's happening?

+7
powershell
source share
2 answers

It looks like an error. Per http://www.vistax64.com/powershell/62805-out-file-force-doesnt-seem-work-advertised.html , this problem has already been submitted to MS Connect.

+8
source share

As Micheal mentioned, it looks like a mistake (or false advertising!).

EDIT: At first, I thought the ">" operator worked, but I made a mistake in my test. This is not as one would expect. However, you can try using a new element instead:

 new-item -force -path $path -value "bar" -type file 

Not exactly the same, but you can create a simple function to do what you want:

 function Out-FileForce { PARAM($path) PROCESS { if(Test-Path $path) { Out-File -inputObject $_ -append -filepath $path } else { new-item -force -path $path -value $_ -type file } } } 
+6
source share

All Articles