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?
Use PowerShell Get-ChildItem cmdlet with -File parameter to filter and get childitems files only. PS C:> Get-ChildItem -Path D:PowerShell -File In the above example, PowerShell get childitem gets all the files from path specified by – Path parameter Output of above PowerShell command, Mode a represent archive.
Get-ChildItem (GCI) gets items and if the item is a container, it will get child items available inside the container. Location specified in PowerShell Get-ChildItem can be file system directory, registry, or certificate store.
To help debug the path try Get-ChildItem -LiteralPathand echo the result, Literal Path takes in the path as it is and be sure not to include any wild cards $Files = Get-ChildItem -Name -LiteralPath "\pdw01rasci001\SSISPackages\PROD\Logs\exec_package_OrderAnalytics_EXTR.dtsx" echo "$Files"
It doesn't matter if I use double or single quotes in the Get-Childitem path or if I use a sub-expression like tfl is talking about. I get the same results (Get-Childitem -path \server\folder1,folder2) instead of two separate Get-Childitems.
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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With