14

This one stumps me a bit. I generally feel pretty advanced in powershell but I simply dont understand the nuance of this one.

This works

$LogFiles = Get-ChildItem -Path c:\windows\temp\*.log,c:\temp\*.log,C:\programdata\Microsoft\IntuneManagementExtension\Logs\*.log

Yet what I want to do (and doesnt work) is this:

$LogsToGather = "c:\windows\temp\*.log,c:\temp\*.log,C:\programdata\Microsoft\IntuneManagementExtension\Logs\*.log"
$LogFiles = Get-ChildItem -Path "$($LogsToGather)" -Recurse

I have tried making the VAR an array, I have tried a number of things with making string. I was able to write around the issue but I am uniquely interested in understanding what data type -path is accepting with that common delineation and be able to create it dynamically.

It seems like a trick that the cmdlet accepts comma delineation. Can it be recreated using some sort of array, hashtable, etc..?

Anyone know?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Eric Weintraub
  • 967
  • 1
  • 10
  • 23
  • The input parameter `-Path` is a string array `[System.String[]]`. So pass your paths also with an array like `[System.String[]]$LogsToGather = @('', '')` – Patrick Oct 16 '19 at 13:58

1 Answers1

25

Yes, $LogsToGather must be an array of strings for your command to work:

$LogsToGather = 'c:\windows\temp\*.log', 'c:\temp\*.log', 'C:\programdata\Microsoft\IntuneManagementExtension\Logs\*.log'

Note how the array elements, separated by ,, must be quoted individually (see bottom section).


Get-Help with -Parameter is a quick way to examine the data type a given parameter expects:

PS> Get-Help Get-ChildItem -Parameter Path

-Path <String[]>
    Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (`.`).

    Required?                    false
    Position?                    0
    Default value                Current directory
    Accept pipeline input?       True (ByPropertyName, ByValue)
    Accept wildcard characters?  false

String[] represents an array ([]) of [string] (System.String) instances - see about_Command_Syntax.

For more information on Get-ChildItem, see the docs.


As for what you tried:

$LogsToGather = "c:\windows\temp\*.log,c:\temp\*.log,C:\programdata\Microsoft\IntuneManagementExtension\Logs\*.log"

This creates a single string that, when passed to Get-ChildItem, is as a whole interpreted as a single path, which obviously won't work.

Note that specifying the elements of an array unquoted, as in:

Get-ChildItem -path c:\windows\temp\*.log, c:\temp\*.log, ...

is only supported if you pass an array as a command argument, not in the context of creating an array with an expression such as $LogsToGather = 'foo', 'bar', ..

The reason is that PowerShell has two fundamental parsing modes - argument mode and expression mode, as explained in this answer,

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 4
    mklement0 your response is nothing short of amazing. Thank you. What I have heard here today will help me now and far into the future. Thanks again! – Eric Weintraub Oct 18 '19 at 18:58