Question:
I’m using the powershell Add-Content
to create a file. However, when the folder of the file does not exist I get an error:
Add-Content : Could not find a part of the path 'C:\tests\test134\logs\test134.log'.
According to the documentation this should create the folder:
1 2 |
PS C:\> Add-Content -Value (Get-Content "test.log") -Path "C:\tests\test134\logs\test134.log" |
This command creates a new
directory and file and copies the content of an existing file to the
newly created file.This command uses the Add-Content cmdlet to add the content. The value
of the Value parameter is a Get-Content command that gets content from
an existing file, Test.log.The value of the path parameter is a path that does not exist when the
command runs. In this example, only the C:\Tests directories exist.
The command creates the remaining directories and the Test134.log
file.
Seems like an clear issue in Add-Content, doesn’t it?
Can you reproduce this?
edit: I’m running PowerShell version 5.1.16299.64
BR Matthias
Answer:
Add-Content
cmdlet can’t create the path but only the file. It works:
1 2 3 4 5 6 |
$Path = "C:\tests\test134\logs2\test134.log" $Path |% { If (Test-Path -Path $_) { Get-Item $_ } Else { New-Item -Path $_ -Force } } | Add-Content -Value 'sample content' |