Scripting Files with PowerShell's Get-Childitem (gci)
Scripting Files with PowerShell's Get-Childitem (gci)
Sooner or later you need a script which lists the files in a folder. In DOS we would type: 'DIR'; the nearest equivalent in PowerShell is gci. The full name behind the alias of gci is the
informative,
get-ChildItem. You can take the comparison further, dir /s in DOS, translates to get-ChildItem -recurse in PowerShell.
When you discover a new
PowerShell command, it benefits from being probed with what I call the 'Trusty Twosome'. Experiment with get-Help and get-Member and unlock new scripting
possibilities for get-Childitem. To see what I mean try these two commands:
1) get-help get-Childitem (help gci) If you prefer abbreviations. (help gci -full) If you
like examples.
get-help unearths useful parameters such as -recurse, -exclude, it also reveals hidden files with: -force.
2) get-Childitem | get-Member (gci | gm) If you enjoy aliases.
(gci | gm -Membertype property) If you are fond of filters.
get-Member reveals properties
that you don't normally see in explorer, for example, CreationTime.
# PowerShell script to list the files in C: root get-Childitem "C:\"
Note 1: In this instance C:\ is the root and get-Childitem lists any files in this directory.
Note 2: get-Childitem "C:\" works equally well without the speech
marks. For example: get-Childitem C:\ However, you do need the backslash after the colon.
Note 1: The key addition is the parameter -force. What this does is include hidden and system files.
Note 2: The additional commands enable us to count the
files, this makes easier to see prove that -force really makes a difference. Double check what I mean by running the script with, and
then without, the -force switch.
Guy Recommends: SolarWinds Engineer's Toolset v10
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates.
There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10
# PowerShell cmdlet to list the System files in the root of C:\ $i=0 $GciFiles = get-ChildItem "c:\" -force |where {$_.attributes -match "System"} foreach ($file in $GciFiles) {$i++} $GciFiles |sort |ft name, attributes -auto Write-host "Number of files:
" $i
Note 1: We need to employ the comparison parameter -match "System", rather than -eq "System", this is because System files also have Hidden and other attributes. Consequently, their
attribute does not equal ''System'', although it does contain, or match the word System.
Note 2: You probably worked it out for your self, but just to emphasise that the variable $i is a counter.
Moreover, ++ increments $i each time the 'where' statements makes a match. Incidentally, omitting $=0 at the beginning produces an unexpected, or undesirable result when you run the script for a
second time.
Challenge 1: Repeat the command with and without -force.
Challenge 2: Substitute this new 'where' clause on line 3: where {$_.attributes -ne
"Directory"}. -ne is the opposite of -eq. Thus this command filters out the directory entry.
# PowerShell cmdlet to list the System files in the root of C:\ $i=0 $GciFiles = gci c:\ -force |where {$_.attributes -ne "Directory"} foreach ($file in $GciFiles) {$i++} $GciFiles |sort |ft name, attributes -auto # $GciFiles |get-Member $i
Outside of PowerShell, recurse is a little known verb meaning rerun. Inside of PowerShell, -recurse is one of the most famous parameters meaning: repeat the procedure on
sub-folders.
The point of this script is to list all the executables in the System32 folder. Moreover it will display the CreationTime, which can help determine if a file is up-to-date. This
technique is particularly useful for troubleshooting .dll files.
# PowerShell script to list the exe files under C:\Windows\System32 $i =0 $DllFiles = gci "C:\Windows\System32" -recurse | ? {$_.extension -eq ".exe"} Foreach ($Dll in $DllFiles) { $Dll.name
+ "`t " + $DLL.CreationTime + "`t " + $Dll.Length $i++ } Write-Host The total number of files is: $i
Note 1: This example uses two aliases; my principle reason was to make line 4 shorter. Gci is an alias for our featured command get-Childitem, and the question mark (?) is
an alias for 'where'.
Note 2: The -recurse parameter comes directly after the name of the directory. For completeness, the location should be introduced by the -path parameter, but as this is
optional, I have omitted the -path parameter in this script. However, this is how to include the -path parameter. $DllFiles = gci -path "C:\Windows\System32" -recurse.
Note 3: If you investigate the file's properties, then you can spot other assets, for
example: LastWriteTime, or even Length. Here is how to employ get-Member to list the properties.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.