-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need examples on when/how to use wild card in path with -Recurse
#10640
Comments
Here is a summary of the de facto behavior and its pitfalls:
|
In light of the above intricacies, I suggest providing the following guidance with respect to use of
For example: Instead of: # -Path implied
Get-ChildItem C:\path\to\package*.json -Recurse use: Get-ChildItem -LiteralPath C:\path\to -Filter package*.json -Recurse This separation provides an unambiguous and conceptually clear solution that avoids the pitfalls above. |
This comment was marked as resolved.
This comment was marked as resolved.
I commented to the original thread in more detail. Would it be safe to assume that these three guidances eliminate unexpected results:
|
In other words: I think the advice in my previous comment is both stringent enough and sufficient. A treacherous non-wildcard scenario: The problem is that the last component - whether it contains wildcard metacharacters or not - is always searched for at every level of the target directory subtree if (a) it matches the name of a file among the immediate children of the target directory or (b) it matches nothing among the immediate children.
# -Path implied
Get-ChildItem .\node_modules -Recurse | Remove-Item -Recurse
Get-ChildItem -LiteralPath .\node_modules -Recurse | Remove-Item -Recurse
Get-ChildItem -LiteralPath . -Filter node_modules -Recurse | Remove-Item -Recurse That is, instead of deleting a single Even though a relative path is used in the above example, the same applies to an absolute one; let's say # !! Same match-at-every-level logic as with .\node_modules, if $HOME happens not to contain node_modules itself.
Get-ChildItem $HOME\node_modules -Recurse | Remove-Item -Recurse Perhaps needless to say, this behavior can have disastrous consequences, with the likelihood increasing the deeper the (possibly implicitly) targeted directory subtree is; in the extreme case, |
Thank you for your scenario. I am carrying it into the cmdlet workgroup for discussion and keep everyone updated here. |
CmdletsWG discussed this. We think the docs should leverage the content @mklement0 provided starting with the scenario of deleting a file recursively and then describing the behavior with each one and recommending the most explicit one to ensure the user understands what will happen. |
Prerequisites
Get-Foo
cmdlet" instead of "New cmdlet."PowerShell Version
5.1, 7.2, 7.3, 7.4
Summary
When using a wildcard as part of
-Path
w/-Recurse
, it might be surprising to users that the wild card applies to-Path
before-Recurse
so it only recurses into paths that match the wildcard rather than recursing into all folders and then applying the path wildcard.Details
PowerShell/PowerShell#20138
Proposed Content Type
Cmdlet Reference
Proposed Title
No response
Related Articles
No response
The text was updated successfully, but these errors were encountered: