Powershell: Creating new directory in WhatIf mode fails

Created on 11 Sep 2020  Â·  9Comments  Â·  Source: PowerShell/PowerShell

General Info

This is a regression from PS 5.

I think it's a bug and not a feature because the behavior is inconsistent between New-Item and Copy-Item.
Also, for -ItemType File it behaves fine, whereas for -ItemType Directory it fails.

Scenario 1

Steps to reproduce

New-Item '\\non-existing-path\newObject' -ItemType Directory -Whatif:$True

Expected behavior

What if: Performing the operation "Create Directory" on target "Destination: \\non-existing-path\newObject".

Actual behavior

New-Item: The specified path is invalid. : '\\non-existing-path\newObject'

Environment data

PSVersion                      7.0.3
PSEdition                      Core
GitCommitId                    7.0.3
OS                             Microsoft Windows 10.0.19041
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Scenario 2

The same problem exists for a valid shared folder when there are no permissions:

Steps to reproduce

New-Item  '\\existing-path-with-no-permissions\newObject' -ItemType Directory -Whatif:$True

Expected behavior

What if: Performing the operation "Create Directory" on target "Destination: \\existing-path-with-no-permissions\newObject".

Actual behavior

New-Item: The user name or password is incorrect. : '\\existing-path-with-no-permissions\newObject'
Issue-Question

Most helpful comment

it's an (undocumented?) change from PowerShell 5, so it seems to be a regression,

Then I would consider a bug that was fixed.

other cmdlets behave differently

Then those other cmdlets should be fixed if possible. Note this may not be possible because as @SeeminglyScience has mentioned it may be a check done by the actual operation not a check by the cmdlet itself.

Ultimately if you’ve asked to do something and are checking what would happen then it should fail if it’s not possible.

All 9 comments

Isn't this the desired behaviour, it's erroring if it's unable to process the path as \\server is not a full UNC path and if you don't have permissions on a dir then with and without -WhatIf would fail with that error.

Isn't this the desired behaviour

Perhaps I simplified the example too much, sorry, I just updated it.

But still, as I said, I don't think it's the desired behavior, at least for these reasons:

  • it's an (undocumented?) change from PowerShell 5, so it seems to be a regression,
  • other cmdlets behave differently, so it's inconsistent (e.g. Copy-Item "C:\" '\\non-existing-path' -Whatif:$True prints What if: Performing the operation "Copy Directory" on target "Item: C:\ Destination: \\non-existing-path". instead of throwing),
  • it's also inconsistent between params of New-Item - it prints What if: ... message for -ItemType File and throws for -ItemType Directory.

If an exception is throw by the code in the cmdlet itself (e.g. as part of input validation), the error will come through. If the exception is surfaced via the underlying API that actually performs the operation (e.g. File.Copy(string, string)) it won't be surfaced in WhatIf mode.

In that way, whether a command emits an error during WhatIf should be considered an implementation detail and not relied on.

Is “the username and password is incorrect” a valid response to the question, “what if i do this thing?”

My personal opinion is that it is a valid answer. It seems more correct than the cmdlet responding with “I’ll do this thing” when the thing is impossible for various reasons.

Is “the username and password is incorrect” a valid response to the question, “what if i do this thing?”

My personal opinion is that it is a valid answer. It seems more correct than the cmdlet responding with “I’ll do this thing” when the thing is impossible for various reasons.

I do agree with you in this regard, but I assume that you'd agree that the presented behavior ideally should be:

  1. a result of a conscious decision,
  2. consistent between:
    a. handling files and directories by the same cmdlet (New-Item -ItemType Directory vs. New-Item -ItemType File),
    b. handling directories by similar cmdlets (New-Item vs. Copy-Item)

it's an (undocumented?) change from PowerShell 5, so it seems to be a regression,

Then I would consider a bug that was fixed.

other cmdlets behave differently

Then those other cmdlets should be fixed if possible. Note this may not be possible because as @SeeminglyScience has mentioned it may be a check done by the actual operation not a check by the cmdlet itself.

Ultimately if you’ve asked to do something and are checking what would happen then it should fail if it’s not possible.

U need to re-find this file.
As like as
URL download link.
_-New file (...)_
_-Copy file (...)_
So...

Maybe I saw in expected behavior

agreeing with @jborean93 here.
The only "promise" -WhatIf gives is that it will not perform any actual changes.
I see no mandate to hide errors that happen in preparatory steps

Was this page helpful?
0 / 5 - 0 ratings