Skip to content

New-Item -Force does not have expected behavior for all types #17656

@tolgabalci

Description

@tolgabalci

Prerequisites

Steps to reproduce

Issue is -Force does not seem to understand junctions. It thinks they are directories and fails to remove them because they are not empty.

Works fine with file types. (see screenshot)

Most types including registry entries allow -Force to overwrite the item. Logically -Force should do the same for a Junction.

Due to -Force not working you must first Remove-Item prior to New-Item, which works perfectly fine. Remove-Item simply removes the junction and does not complain that it is not empty. New-Item -Force needs the same intelligence.

Expected behavior

PS> New-Item MyWindows -Type Junction -Value C:\Windows\
PS> New-Item MyWindows -Type Junction -Value C:\Windows\ -Force
PS>

Actual behavior

PS> New-Item MyWindows -Type Junction -Value C:\Windows\
PS> New-Item MyWindows -Type Junction -Value C:\Windows\ -Force
New-Item: Directory C:\Temp\MyWindows cannot be removed because it is not empty.
PS>

Error details

Exception             :
    Type    : System.IO.IOException
    Message : Directory C:\Temp\MyWindows cannot be removed because it is not empty.
    HResult : -2146232800
TargetObject          : C:\Temp\MyWindows
CategoryInfo          : WriteError: (C:\Temp\MyWindows:String) [New-Item], IOException
FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.NewItemCommand
InvocationInfo        :
    MyCommand        : New-Item
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : 5
    Line             : New-Item MyWindows -Type Junction -Value C:\Windows\ -Force
    PositionMessage  : At line:1 char:1
                       + New-Item MyWindows -Type Junction -Value C:\Windows\ -Force
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    InvocationName   : New-Item
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo :

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.5
PSEdition                      Core
GitCommitId                    7.2.5
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestPotential candidate to participate in HacktoberfestIssue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions