Creating new directory in WhatIf mode fails
See original GitHub issueGeneral 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:
- Created 3 years ago
- Comments:10 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Then I would consider a bug that was fixed.
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.
agreeing with @jborean93 here. The only “promise”
-WhatIfgives is that it will not perform any actual changes. I see no mandate to hide errors that happen in preparatory steps