question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creating new directory in WhatIf mode fails

See original GitHub issue

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 Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
jborean93commented, Sep 11, 2020

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.

1reaction
FriedrichWeinmanncommented, Sep 15, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

New-Item (Microsoft.PowerShell.Management)
The New-Item cmdlet creates a new item and sets its value. The types of items that can be created depend on the location...
Read more >
How to create a directory if it doesn't exist using Node.js
Just open the file and handle the error when it's not there. Since we're talking about a directory rather than a file, this...
Read more >
Create a new folder and set permissions with PowerShell
The PowerShell script discussed in this post allows you to create a new folder, remove the inherited permissions, and set new permissions ...
Read more >
Powershell Create Directory If Not Exist: A Detailed Guide
We have demonstrated how to create directories using PowerShell with the New-Item cmdlet, applying the -ItemType parameter, utilizing the -Force parameter, and ...
Read more >
The Ultimate Guide to Robocopy
Learn all of the robocopy commands that you can use in data migration, syncing folders, and copying even large files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found