Windows PowerShell


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.

get-ChildItem Topics

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:

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.

Example 1 - List files in the root of the C:\ drive

Here is an example which lists 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 - 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 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 without, the -force switch.

Example 3 - 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 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

  ˚

Example 4 - The famous -recurse parameter

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.

# 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

Copy-Item
get-Item (gi)
Move-Item
New-Item
Remove-Item
Set-Item

See more PowerShell real life tasks

PowerShell Home  • Real life tasks  • Eventlog  • Exchange  • Com objects  • Services  • Syntax

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.

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.

 *


Google

WebComputerperformance.co.uk

 

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

Please report a broken link, or an error.