PowerShell 3.0 Get-Childitem (gci)

What's New with PowerShell 3.0 Get-Childitem?PowerShell 3.0 Get-ChildItem New Parameter

In PowerShell v 3.0 the Get-ChildItem cmdlet has new parameters.  You can list just folders (and not files) by appending -Directory.  I also like the -Attributes parameter, which can filter system or hidden files.

Windows PowerShell 3.0 Get-ChildItem Topics

 ♣

Example 1: List Only Windows Folders with -Directory

The scenario: You just want a list of the folders or directory names, you don't want to be snowed under with file names.

The solution: -Directory. Simply append this parameter to the regular Get-ChildItem command thus:

# PowerShell 3.0 GCI -Directory Parameter
Get-Childitem C:\Windows -Directory

Note 1: Just in case you installed the operating system at an usual location employ WMI to find the SystemDirectory.

# PowerShell 3.0 Get-ChildItem -Directory Parameter
$WinDir = (Get-WmiObject Win32_OperatingSystem).SystemDirectory
GCI $WinDir -Directory

Note 2: This example uses GCI, which is the famous alias for Get-ChildItem.

Note 3: You could swap -File for -Directory and contrast the results.

Note 4: You could specify the path with $Env:WinDir instead of using WMI.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's 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. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Example 2: Get-ChildItem -Attributes

DOS experts found PowerShell 2.0 frustrating because there were commands you could do with DIR that you could not achieve with GCI.  Now that deficiency has been rectified, for instance see how easily you can filter lists of files by attribute.

# PowerShell 3.0 GCI -Attributes Parameter
Clear-Host
Get-ChildItem C:\Windows\System32 -Attributes hidden

Note 4:  Whereas PowerShell cmdlets all have singular nouns, plurals crop-up amongst the parameters.  My point, -Attributes is plural and this led me to believe you could filter on multiple attributes.

# PowerShell 3.0 GCI -Attributes using and
Get-ChildItem C:\ -Attributes hidden+ReadOnly

Note 5:  I also stumbled across the handy 'Not' operator, it was the exclamation mark (!).  However, you have to precede it with the and operator, thus (+!)   Here is an actual example:

# PowerShell 3.0 GCI -Attributes and Not
Get-ChildItem C:\ -Attributes hidden+!ReadOnly

Note 6:  This displays files that are hidden but NOT read only.

PowerShell 3.0 Get-ChildItem Intellisense ResearchPowerShell 3.0 Get-ChildItem New Attributes

Thanks to the new PowerShell intellisense you can easily research parameters for cmdlets such as Get-ChildItem.

The secret is to type the cmdlet's name slowly, and watch-out for little context menus to appear on screen.  For example, see what happened when I typed GCI C:\windows\system32, and then as I typed a dash -

... see screenshot to the right.

See my PowerShell Get-File function.

Example 3: Old Style Method of Listing System Files

PowerShell v 2.0 lacks the Attributes parameter thus it's more difficult to list files based on their System, Hidden or ReadOnly value.  One way would be to use a Where-Object clause, but PowerShell 3.0's -Attribute method is much more efficient.

#PowerShell 2.0 Method of listing files based on attribute
Get-ChildItem C:\ -force | Where {$_.Mode -Like '*s*' }

Note 7: Here is where you can see more about PowerShell -Recurse and -Force parameters.

Guy Recommends:  SolarWinds' Free Bulk Import ToolFree Download Solarwinds 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.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

PowerShell 3.0 Where-Object with GCI

Firstly, I digress in order to the cmdlets properties with Get-Member (GM)

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

Note 8: The point is we can see there is a file property called 'Extension'.  Incidentally GM (Get-Member) reveals properties that you don't see in explorer, for example, CreationTimeUtc.

Scenario: We want a list of the dynamic link library (.dll) files in the System32 folder.

Method: We employ a Where-Object (alias 'Where') clause.

# PowerShell 3.0 File Properties Get-Childitem Where
Get-Childitem C:\Windows\System | Where Extension -eq .dll

Problem with PowerShell 3.0's Where Clause

Following the above example you would think that | Where CreationTime.Year -gt 2010 would provide a list of files that were born in 2011, but it doesn't.

# PowerShell 3.0 Where Problem
Get-Childitem C:\Windows\System | Where CreationTime.Year -gt 2010

Note 9: Although the above example completes without error it does not display any files.  Just in case there are no matching files, change -gt to -lt.  Still no files appear.

Solution: Revert to the 'Old' PowerShell v 2.0 Where syntax, complete with {Curly brackets}, $_. and 'single speech mark'.

# PowerShell 3.0 File Properties Solution
Get-Childitem C:\Windows\System | Where {$_.CreationTime.Year -gt '2010' }

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer's Toolset v10

This Engineer's Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it's like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer's Toolset now!

Download your fully functional trial copy of the Engineer's Toolset v10

Get-ChildItem Has Three Aliases

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

GCI
LS  (Old list)
DIR (For DOS die-hards)

See examples of PowerShell v 3.0 foreach »

Summary of PowerShell 3.0 Get-ChildItem New Features

PowerShell 3.0 brings has extra parameters to many cmdlets, for example DOS lovers missed the equivelent of Dir *. in PowerShell v 2.0's Get-ChildItem.  I also like the -Attributes parameter to filter ReadOnly files.  Learn more for yourself through the Intellisense context sensitive menus.

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

 


See more Microsoft PowerShell v 3.0

PowerShell 3.0  • What's New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: