Repository navigation
Closed
Labels
Area-FileSystem-Providerspecific to the FileSystem providerBreaking-Changebreaking change that may affect usersResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Get-ChildItem -Path $nullExpected behavior
Some kind of error similar to other *Item CmdLets when Path is $null
Remove-Item -Path $null
Remove-Item: Cannot bind argument to parameter 'Path' because it is null.Looking at the source this is not unexpected, but it can have some unexpected consequences.
Consider the following:
I have two folders setup like this:
C:\temp\saveeverythinghere> Get-ChildItem
Directory: C:\temp\saveeverythinghere
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2022-07-28 15:08 0 SaveEverythingHere1.txt
-a--- 2022-07-28 15:08 0 SaveEverythingHere2.txt
-a--- 2022-07-28 15:08 0 SaveEverythingHere3.txt
C:\temp\saveeverythinghere> Get-ChildItem ..\deleteme\
Directory: C:\temp\deleteme
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 2022-07-28 15:08 0 DeleteMe1.txt
-a--- 2022-07-28 15:08 0 DeleteMe2.txt
-a--- 2022-07-28 15:08 0 DeleteMe3.txtNow run this code:
$ScanExtensions = '.ps3'
$PathToDeleteFilesFrom = 'C:\temp\deleteme'
# This whill return 0 files
$ScanFiles = Get-ChildItem $PathToDeleteFilesFrom -File -Recurse | Where-Object -Property Extension -in $ScanExtensions
# This will now remove _all_ files in $PWD
Get-ChildItem $ScanFiles.FullName | Remove-Item(In this very hypothetical scenario we can solve it using Set-StrictMode -Version 7.2 to get $ScanFiles.FullName to throw an error, but I still think it's unexpected.)
Actual behavior
Get-ChildItem -Path '.'Environment data
PSVersion 7.2.5
PSEdition Core
GitCommitId 7.2.5
OS Microsoft Windows 10.0.22622Visuals
Reacted by Emanuel Palm and MartinGC94
Metadata
Metadata
Assignees
Labels
Area-FileSystem-Providerspecific to the FileSystem providerBreaking-Changebreaking change that may affect usersResolution-Won't FixThe issue won't be fixed, possibly due to compatibility reason.WG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Activity
jhoneill commentedon Jul 28, 2022
Unlike
remove-item(rm/del) andcopy-item(cp/copy) TheGet-itemandGet-ChildItemcommands (dir/ls) work without any parameters so inside the command path being null/empty selects the current directory.This does not, of itself, prevent the
Pathparameter from rejecting null/empty input. However there will be many things out there which look like thisBy not rejecting null/empty values Get-ChildItem saves whoever writes that script from having to either default
$MyPathParmeterto $pwd, or change how Get-ChildItem is called depending on whether the parameter is passed. I would expect many scripts to rely on this behaviour.Of course if the line is
Get-ChildItem $MyPathParameter | remove-itemthe way of working we have requires the script author to check that the value isn't null or empty.fflaten commentedon Jul 28, 2022
It's a breaking change.
ls $nonExistingVarin bash anddir ""(not entirely the same) in cmd behaves the same.MartinGC94 commentedon Jul 28, 2022
James O'Neill (@jhoneill) I'm sure someone has done it intentionally but I doubt it's common.
Most built-in Get commands don't allow you to provide a null value to the "target" parameter, Get-Disk, Get-Process, Get-Service, Get-NetAdapter, Get-Vm all fail if you try to use null. I honestly can't think of any command besides
Get-ChildItemthat has this odd behavior.When basically every other command forces the user to validate, or set a default value in their function I would expect most users to develop a habit of doing it like this.
IMO it's more likely that someone is negatively impacted by this due to a variable typo or whatever than someone intentionally wanting/expecting it to default to $pwd when given a null value.
bjompen commentedon Jul 28, 2022
I am very much aware of this as it changes a default behaviour. In fact, I do not really expect this behaviour to change or be fixed, but I am very curious as to why this decision was made.
James O'Neill (@jhoneill)

Get-Itemnot only does not have the same behaviour.It also makes a difference between $null and [string]::Empty, something I think Get-ChildItem also could do (Assuming there is a valid reason to list folder contents in a folder with no name for example.)
jhoneill commentedon Jul 28, 2022
Sorry brain fade on my part. Of course only Get-Childitem works with no path.
Get-ChildItem could have been designed to reject null or empty, but it wasn't. And over the last 15 years a decent number of scripts have been written which depend on the present behaviour. So this is breaking change where the benefit is small. (MartinGC94 we can argue how big the number is but the example I gave of one natural way to write it - i.e. not splatting ps bound params will pass an empty string if the parameter is not specified, and so will be break if empty is rejected, so I think its a breaking change which would have quite a wide effect)
vexx32 commentedon Aug 9, 2022
WG-Engine discussed this today.
The original intent here (if there was any) is not fully remembered, but folks echoed the points raised before here around similarity to
lsordircommands that behave similarly.Given the change here would be a breaking one, and that it is generally a bit of an edge case, we aren't confident we should go ahead and fix this. It's also mostly a non-issue; it's fairly trivial to do one's own validation in a script before calling Get-ChildItem in most cases, either with an if statement or a Where-Object in a pipeline to discard or otherwise ignore null values.
We understand the discomfort for inconsistency and occasional confusion this behaviour can sometimes cause, but our consensus is that the benefit from the change here isn't compelling enough for this breaking change to be acceptable.