Home > Software engineering >  Is there a difference between Get-Item ending with a trailing wildcard and Get-ChildItem without a t
Is there a difference between Get-Item ending with a trailing wildcard and Get-ChildItem without a t

Time:12-18

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-Item is designed to return information about the targeted item(s) themselves.

  • Get-ChildItem is 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.exe only comes with its internal dir command.

  • Unix-like platforms come only with the external ls utility.

    • Note: Arguably, it is the external stat utility that is the analog of Get-Item, but its syntax and output format differ substantially from ls. 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 various Format-* cmdlets allowing customization of the representations on demand.

For Get-ChildItem to fully replace Get-Item, it would need to support the following additional features:

  • On Windows, the -Stream parameter, which only Get-Item currently supports.

  • Hypothetically, the -Credential parameter (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 ls utility's -d option does that - albeit without performing name resolution, such as resolving . to the directory's name.
    • By contrast, cmd.exe's dir command does not directly support that (in other words: it doesn't directly support Get-Item's functionality for directories).
  • Related