As an example, compare the following commands in Powershell:
Get-Item -Path "C:\Users\test.user\Desktop\*"
and
Get-ChildItem -Path "C:\Users\test.user\Desktop"
My limited testing indicates that there is no difference in the output.
If there is indeed no difference, this begs the question: Why does Get-ChildItem exist?
CodePudding user response:
Before discussing whether two distinct commands are needed:
Get-Itemis designed to return information about the targeted item(s) themselves.Get-ChildItemis designed to return information about the targeted item's children, if a given item happens to be a container - which in the case of a file-system item applies to directories (folders).- However, items targeted with a wildcard expressions are always reported as themselves, even if they are containers (directories).
Undoubtedly, there is substantial overlap between those two cmdlets, with Get-ChildItem supporting many more (partially provider-specific) features than Get-Item. Conversely, there are a few features exclusive to Get-Item.
True: There is no strict need for both Get-Item and Get-ChildItem.
Other shells and platforms make do with just one command, which is the equivalent of Get-ChildItem, however, not of Get-Item:
cmd.exeonly comes with its internaldircommand.Unix-like platforms come only with the external
lsutility.- Note: Arguably, it is the external
statutility that is the analog ofGet-Item, but its syntax and output format differ substantially fromls. That said, a differing output format needn't be a concern in PowerShell, given that it has a clear separation between data and its representation, with the variousFormat-*cmdlets allowing customization of the representations on demand.
- Note: Arguably, it is the external
For Get-ChildItem to fully replace Get-Item, it would need to support the following additional features:
On Windows, the
-Streamparameter, which onlyGet-Itemcurrently supports.Hypothetically, the
-Credentialparameter (according to the linked docs, it is currently not supported by any of the providers that ship with PowerShell).A (somewhat self-contradictory) way to indicate that you want a literal target path that refers to a directory (container) to return information about the directory (container) itself rather than its children.
- On Unix-like platforms, the
lsutility's-doption does that - albeit without performing name resolution, such as resolving.to the directory's name. - By contrast,
cmd.exe'sdircommand does not directly support that (in other words: it doesn't directly supportGet-Item's functionality for directories).
- On Unix-like platforms, the
