Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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 gci alias is the informative Get-ChildItem.  You can take the comparison further, dir /s in DOS, translates to Get-ChildItem -recurse in PowerShell.

PowerShell Get-ChildItem Topics

 ♣

Example 1 - To List Files in the Root of the C:\ drive

Here is an example to get started, it will list all the files in the C:\ root.  If you need any help in executing the code, then see here.

# 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.

Example 2 - The Famous GCI -Recurse Parameter

Outside of PowerShell, recurse is a little known verb meaning rerun.  Inside of PowerShell, Get-ChildItem -recurse is one of the most famous parameters meaning: repeat the procedure on sub-folders.

The point of example 2b is to list all the dll's under the Windows 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.

Example 2a - The Problem

Our goal is to find all the dlls under the Windows folder.  The problem is that this script does not search the system32 subdirectory.

# PowerShell gci example to list .dll in Windows folder
$Files = gci "C:\Windows\" | Where {$_.extension -eq ".dll"}
$Files | Format-Table Name, CreationTime, Length -auto

Note 3: This script lists only 4 dlls; surely there must be more?

Note 4: I substituted the alias gci for Get-ChildItem

Example 2b - The Solution

To find .dlls in the subdirectories let us append the Get-ChildItem -recurse parameter.

# PowerShell example to list .dll in Windows folder and sub-folders
Write-Host `n "Waiting ...."
$Files = gci "C:\Windows\" -recurse | Where {$_.extension -eq ".dll"}
Clear-Host
$Files | Format-Table Name, CreationTime, Length -auto

Note 5: I changed Name to FullName so that it would display the subdirectory where the file cold be found.  See more on $_.extension.

Example 2c - Extra Formatting

This example produces the same result but uses the -f formatting command and the alias Gci.

# PowerShell script to list the dll files under C:\Windows\System32
$i =0
$Files = Gci "C:\Windows\" -recurse | ? {$_.extension -eq ".dll"}
Foreach ($Dll in $Files) {
"{0,-28} {1,-20} {2,12}" -f `
$Dll.name, $DLL.CreationTime, $Dll.Length
$i++
}
Write-Host The total number of dlls is: $i

Note 6:  This example also uses two aliases; my principle reason was to make line 3 shorter.  Gci is an alias for our featured command Get-Childitem, and the question mark (?) is an alias for 'where'.

Note 7:  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.
$Files = gci -path "C:\Windows\System32" -recurse.

Guy Recommends: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.  Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server.

Download your free copy of WMI Monitor

Trusty Twosome (Get-Help and Get-Member)

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:

# Research PowerShell Get-Childitem Parameters
Get-Help Get-Childitem

 # (Try help gci) If you prefer abbreviations.
# (And help gci -full) If you like examples.

Note 8:  Get-Help unearths useful parameters such as -recurse, -exclude, it also reveals hidden files with: -force.

# Research PowerShell Get-Childitem Properties
Get-Help Get-Childitem

Get-Childitem | Get-Member
# (gci | gm) If you enjoy aliases.
# (gci | gm -Membertype property) If want only properties

Note: Get-Member reveals properties that you don't normally see in explorer, for example, CreationTime.

Example 3 - List ALL Files, Including Hidden and System

$i=0
$GciFiles = Get-Childitem c:\ -force
foreach ($file in $GciFiles) {$i++}
$GciFiles | sort | ft name, attributes -auto
Write-host "Number of files: " $i

Note 9: The key addition is the parameter -force.  What this does is include hidden and system files.

Note 10: 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.

See basic PowerShell examples featuring Get-ChildItem

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v10

Solarwinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

Example 4 - Filter to List Just the System files

# 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 11:  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 12:  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

 

# PowerShell script to investigate file properties
Get-ChildItem | Get-Member -Membertype property

Get-Childitem -recurse can be surprisingly tricky, if this construction is giving you problems, see here for more help on -recurse

The Rest of the Item Family

# PowerShell Item Cmdlet Research
Clear-Host
Get-Command -noun Item

As expected there is an Get-Item cmdlet, but you may not realize there is a Move-Item cmdlet.  PowerShell -noun or -verb research always throws up at least one surprise.

Copy-Item
Get-Item (gi)
Move-Item
New-Item
Remove-Item
Set-Item
Also, a special mention of Test-Path

Summary of Get-ChildItem Alias 'Gci'

With Microsoft, there are always at least three ways of doing everything, what seems like redundancy when you are an expert, seems like perspective when you are a beginner.  Get-ChildItem has not one, but three aliases check thus:

# PowerShell Alias GCI
Get-Alias -definition Get-ChildItem

If you like this page then please share it with your friends

 


See more Microsoft PowerShell file tutorials:

PowerShell Home   • Add-Content  • Get-Content  • Set-Content  • Test-Path  • -recurse

Files  • PowerShell Registry  • PowerShell ItemProperty  • Compare-object

Get-Credential   • Windows PowerShell

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.