I need help with this Get-ChildItem script.

IT-User9733 71 Reputation points
2021-12-14T19:19:12.953+00:00

Hi I need help with this Get-ChildItem script.

I believe that it was created to look for an instance of the JndiLookup.class in the C:\

gci 'C:\' -rec -force -include *.jar -ea 0 | foreach {select-string "JndiLookup.class" $_} | select -exp Path

This is how I interpret it.

get child item, recurse all directories and inc hidden and system files, look for all .jar files, and continue on any errors.
select string "JndiLookup.class", and select all the current attributes and export the path to the screen.

Am I wrong?

Thanks

Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-12-14T19:41:01.24+00:00

    You'd be correct if you didn't use the "-Include" parameter. Once you use that you have to change the "-Path" parameter value to include, say, an asterisk in this case.

    Also, the Get-ChildItem cmdlet doesn't produce a string, it creates an object.

    This is probably what you'd want:

    Get-ChildItem -Path C:\* -Recurse -Force -Include *.jar -ErrorAction 0 | 
        Where-Object {$_.Name -eq "JndiLookup.class"} | 
            Select-Object -Expand Path
    

  2. Limitless Technology 39,916 Reputation points
    2021-12-15T16:33:03.913+00:00

    Hi @IT-User9733

    No, you need a force parameter to display hidden files.
    You can get all items directly within a folder by using Get-ChildItem. Add the optional Force parameter to display hidden or system items.

    Get-ChildItem can filter items with its Path, Filter, Include, and Exclude parameters, but those are typically based only on name. You can perform complex filtering based on other properties of items by using Where-Object.

    Get-ChildItem doesn't display empty directories. When a Get-ChildItem command includes the Depth or Recurse parameters, empty directories aren't included in the output.

    Hope this resolves your Query!!

    ---------------

    --If the reply is helpful, please Upvote and Accept it as an answer--


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.