Powershell Add-Content should create path but throws exception “Could not find a part of the path”

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:

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.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-content?view=powershell-5.1

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:

Source:

Powershell Add-Content should create path but throws exception “Could not find a part of the path” by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply