PowerShell Scripting - Get-ChildItem

前端 未结 3 609
无人共我
无人共我 2020-12-21 02:25

I\'ve written a script that will be used for archiving log files from a server. I\'m in pretty good shape with everything but the recursiveness or not of Get-ChildItem...

相关标签:
3条回答
  • 2020-12-21 03:07

    This is expected behaviour, but admittedly confusing. From the Get-ChildItem help file:

    -Include <string[]>
    

    Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\ Windows directory.

    ps> help dir -full | more

    Hope this helps,

    -Oisin

    0 讨论(0)
  • 2020-12-21 03:21

    The answer is in the full description of the command (get-help get-childitem -full):

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

    So the following would work without recurse.

    PS C:\foo> Get-childitem -path "c:\foo\*" -Include *.txt
    
    0 讨论(0)
  • 2020-12-21 03:21

    I can't tell you the exact why of it (but I will keep looking), but the behavior is documented in the Get-Help for Get-ChildItem:

    -Include <string[]>
        Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path elem
        ent or pattern, such as "*.txt". Wildcards are permitted.
    
        The Include parameter is effective only when the command includes the Recurse parameter or the path leads to th
        e contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\
        Windows directory.
    
    0 讨论(0)
提交回复
热议问题