When you want a PowerShell command to search sub-directories
-Recurse is a life saver. In other contexts this concept is called
iteration, or sub-directory recursion. The cmdlet which benefits
most
from the -Recurse parameter is
Get-Childitem.
Our mission is to list all the Windows files under the Program Files
folder. It was the positioning of -Recurse that gave me my biggest
headache. My tactical error was to try and introduce -Recurse into
a long statement. What I should have done was take my own advice
and build up gradually like this:-
Stage 1 Problem: The script lists files only in the top
level directory.
# PowerShell With Just Get-ChildItem (no recurse) Clear-Host Get-ChildItem -path
"C:\Program Files\"
Note 1: Get-Childitem is the equivalent of dir. In fact PowerShell creates an
alias called dir, thus this old command still works on the command line.
Stage 2 Solution: -Recurse drills down and finds lots
more files.
Note 2: The key to -Recurse is the position, it has to be
directly after the directory. In this example I have explicitly used
-path to define the location.
Stage 3a Precise Solution: -Recurse with a
filter and wildcard* on the directory name.
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Network Performance Monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
What I like best is the way NPM suggests solutions to network
problems. Its also has the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating
network maps, then I recommend that you try NPM now.
I only include this example to explain how difficult it can be to decide where to place
-Recurse. When you study that long middle line you can see
how it's possible to group the output and select a custom Format-Table (ft).
# PowerShell script to find executable in the Windows folder Clear-Host
$Path = "C:\Windows\System32" Get-Childitem $Path -Recurse |
where {$_.Extension -Match "exe"}` | ft -group {$_.Path} Directory, Name
-autosize
Note 4: You can improve this script by adding -ErrorAction
to skip the error message on protected or in use executables.
# PowerShell script to find executables in the Windows\System32
folder Clear-Host $Path = "C:\Windows\System32"
Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue
|`
where {$_.Extension -Match "exe"} | ft -group {$_.Path} Directory,
Name -auto
Here is an utility where you can review firewall settings such as
access control lists (ACL), or troubleshoot problems with network
address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include
managing requests to change your firewall settings, testing firewall
rules before you go live, and querying settings with the browser's
powerful search options.
The problem with the -Recurse parameter is that it only works where the
object has leaf items, for example: C: \Windows\ -Recurse.
My point is, I was caught out by trying C: \Windows\*.dll -Recurse.
To see my point try these two examples:
# PowerShell recurse finds executables under the Windows folder
Clear-Host $Path = "C:\Windows\*.exe" $WinExe =
Get-ChildItem $Path -Recurse $WinExe.count
Note 5: The answer was only about 17. This
script lists .exe files only in the actual Windows folder. -Recurse
is useless here.
# PowerShell script to find ALL executables under Windows folder
Write-Host "Waiting for -Recurse ..." $Path = "C:\Windows\"
$WinExe = Get-Childitem $Path -Recurse -ErrorAction SilentlyContinue `
| Where-Object {$_.Extension -Match "exe"} Clear-Host $WinExe.count
Note 6: Expected answer over 2,000.
-Recurse
does its job. 'Where-Object' plays its role in filtering.
Alternatively, you could employ the -Include parameter.
Guy Recommends: SolarWinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the top row, and save as .csv file.
Then launch this FREE utility and match your fields with AD's
attributes, click and import the users.
While -Recurse works nicely for the above Get-Childitem, I emphasise CHILDitem. I could neither get it
to work with Get-Item, nor could I see -Recurse amongst the parameters for plain Get-Item.
Incidentally, Get-Childitem | Get-Member does not, repeat not, list
-Recurse. This is because -Recurse is a
parameter, or what I call a switch. Get-Member lists methods and
properties, but not parameters, to see more about -Recurse you need:
Case Study The mission is to list all files containing the word 'Microsoft' in the Windows folder or its sub-folders. For this we use the select-string pattern matching command.
The
problem is that this script does not work. All that happens is that we get an error: Cannot find path... Path does not exist.
Note 7: When we add -Include *.txt the cmdlet works as initially planned. Actually, you could swap the famous *.* for *.txt, thus : -Include *.*
Note
8: -Include only works when you
also append the -Recurse parameter.
Note 9: -Include applies to the filename and extension. Where I made a mistake was thinking that -Include would apply to the path.
Simplification There is no reason why you cannot simplify the above
example by removing at least two of the variables, especially on a production script. The only reason that I employed $Path and $StringText is
that when I am creating a script I like to isolate and control each step of the
way.
-Recurse is a classic switch, which instructs PowerShell commands such as
Get-ChildItem to repeat in sub directories. Once you remember that
-Recurse comes directly after the directory, then it will serve you well
in scripts that need to drill down to find information.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is
most useful for PowerShell scripting.
SolarWinds
have produced this
Free WMI Monitor to take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.