tl;dr
Use the following, streamlined version of the command, which should also perform much better.
# Run WITH ELEVATION (as admin):
gci C:\ -rec -file -force -filter *.jar -ev errs 2>$null | # Use -filter, not -include
select-string "JndiLookup.class" | # Pipe directly to select-string
select -exp Path
Note: -ea 0
- short for: -ErrorAction SilentlyContinue
- should normally silence any error messages, but if that doesn't work for you for some reason, 2>$null
should be effective.
-ev errs
- short for: -ErrorVariable errs
- collects all errors that occur in variable $errs
, which you can examine after the fact to determine whether the errors are an indication of an actual permission problem.
Errors are expected in Windows PowerShell, even when running with elevation, namely relating to hidden system junctions, discussed below. However, you can ignore these errors.
In PowerShell (Core) 7+, where these errors no longer occur, you could omit -ev errs 2>$null
above. Any errors that surface then would be indicative of a true permission problem.
Background information:
In general, even running with elevation (as admin) doesn't guarantee that all directories can be accessed. File-system ACLs at the directory level can prevent even elevated processes from enumerating the directory's files and subdirectories.
Notably, there are several hidden system junctions (links to other directories), defined for pre-Vista backward-compatibility only - such as C:\Documents and Settings
and C:\Users\<username>\My Documents
- that even elevated processes aren't permitted to enumerate the children of.
- During file-system enumeration, this fact only becomes apparent in Windows PowerShell, which reports access-denied errors for these junctions. PowerShell (Core) 7+, by contrast, quietly skips them.
- Even in Windows PowerShell the problem is only a cosmetic one, because these junctions merely point to directories that can be enumerated with elevation and therefore are with a
-Recursive
enumeration of the entire drive.
To find all these hidden system junctions:
# Run WITH ELEVATION (as admin):
cmd /c dir c:\ /s /b ashdl
Additional information is in this answer.