diff --git a/src/System.Management.Automation/engine/SessionStateContent.cs b/src/System.Management.Automation/engine/SessionStateContent.cs
index 6e845601d..37bff16ef 100644
--- a/src/System.Management.Automation/engine/SessionStateContent.cs
+++ b/src/System.Management.Automation/engine/SessionStateContent.cs
@@ -1025,4 +1025,3 @@ namespace System.Management.Automation
}
#pragma warning restore 56500
-
diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
index 903c752ea..8267c5e7a 100644
--- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs
+++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs
@@ -6605,6 +6605,13 @@ namespace Microsoft.PowerShell.Commands
path = NormalizePath(path);
+ if (Directory.Exists(path))
+ {
+ string errorMsg = StringUtil.Format(SessionStateStrings.ClearDirectoryContent, path);
+ WriteError(new ErrorRecord(new NotSupportedException(errorMsg), "ClearDirectoryContent", ErrorCategory.InvalidOperation, path));
+ return;
+ }
+
try
{
#if !UNIX
diff --git a/src/System.Management.Automation/resources/SessionStateStrings.resx b/src/System.Management.Automation/resources/SessionStateStrings.resx
index c0ced013d..c0307f876 100644
--- a/src/System.Management.Automation/resources/SessionStateStrings.resx
+++ b/src/System.Management.Automation/resources/SessionStateStrings.resx
@@ -284,7 +284,7 @@
Unable to write content because it is a directory: '{0}'.
-
+
There is no location history left to navigate backwards.
@@ -297,6 +297,9 @@
Attempting to perform the ClearContent operation on the '{0}' provider failed for path '{1}'. {2}
+
+ Unable to clear content of '{0}' because it is a directory. Clear-Content is only supported on files.
+
The dynamic parameters for the ClearContent operation cannot be retrieved from the '{0}' provider for path '{1}'. {2}
diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1
index b21a744a0..93767086f 100644
--- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1
+++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clear-Content.Tests.ps1
@@ -105,6 +105,10 @@ Describe "Clear-Content cmdlet tests" -Tags "CI" {
}
Context "Proper errors should be delivered when bad locations are specified" {
+ It "should throw when targetting a directory." {
+ { Clear-Content -Path . -ErrorAction Stop } | Should -Throw -ErrorId "ClearDirectoryContent"
+ }
+
It "should throw `"Cannot bind argument to parameter 'Path'`" when -Path is `$null" {
{ Clear-Content -Path $null -ErrorAction Stop } |
Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ClearContentCommand"