PowerShell Get-ChildItem Tutorial

Paul Hill

December 28, 2023 • 5 min read

    The “Get-ChildItem” cmdlet in PowerShell is a key tool for listing and managing items within file systems and directories. It’s designed to help users efficiently navigate and manipulate their file system. The Get-ChildItem provides a more powerful and flexible way to navigate and manipulate data in PowerShell environments.

    In this post, our focus will be on demonstrating how “Get-ChildItem” can selectively list either files or directories. This feature is invaluable for specific file system operations, including system audits, organizing data, or processing multiple files simultaneously.

    We’ll cover the cmdlet’s parameters that allow you to refine your listings to either files or directories. Through practical examples, we’ll illustrate how these functions can be applied effectively in various scenarios.

    If you’re interested in our structured training and taking your PowerShell skills to the next level, then check out our full PowerShell course:

    Course: Administration and Automation with Windows PowerShell

    Windows PowerShell helps you automate the management of Active Directory users, the management of VM…

    99 Lessons
    2 Labs
    13 Hr

    Get-ChildItem Cmdlet

    The PowerShell Get-ChildItem Cmdlet syntax is straightforward, yet it offers a range of options to tailor its output to your specific needs. The cmdlet can be invoked simply as Get-ChildItem, or through its aliases gci, dir, and ls, providing a comfortable transition for users from different scripting backgrounds.

    Key Features:

    • Listing Items: At its core, “Get-ChildItem” provides a detailed list of all items in a given directory. This includes files, subdirectories, and hidden items.
    • Path Specification: You can specify the path where you want to list the items. If no path is given, it defaults to the current directory.
    • Filtering Capabilities: One of the cmdlet’s strengths is its ability to filter results. You can use parameters to list only files, only directories, or items that match certain criteria.

    The Cmdlet allows for the following argument:

    ArgumentDescription
    -Path <String[]>Specifies the path(s) where the cmdlet should search for items. If omitted, it defaults to the current directory. Multiple paths can be provided, separated by commas.
    -Filter <String>Filters items based on a provided search string. For example, -Filter "*.txt" will list all text files.
    -Include <String[]>Lists items that match the specified names or paths, used in conjunction with -Path. E.g., -Include "*.txt" will list text files in the specified path.
    -Exclude <String[]>Excludes specified items from the results. For instance, -Exclude "*.log" omits log files from the listing.
    -RecurseSearches items in all child directories of the specified path(s), not just in the immediate directory.
    -ForceShows hidden and system items, which are not displayed by default.
    -NameReturns only the names of the items, excluding other details like the full path.
    -Attributes <FileAttributes>Filters files based on specified attributes, such as ReadOnly, Hidden, etc. -Attributes Hidden lists only hidden files.
    -DirectoryLists only directories, excluding files.
    -FileLists only files, excluding directories.
    -HiddenIncludes hidden items in the output, which are usually not listed by default.
    PowerShell Get-ChildItem Arguments

    You can use these arguments (pick and chose what you need) to fit whatever problem you’re trying to solve. You can output this same table in PowerShell by using the Get-Help Get-ChildItem cmdlet.

    Get-ChildItem Examples

    In this section, we’ll explore ten of the most common and practical examples of using the Get-ChildItem cmdlet. These examples will demonstrate the versatility and power of this cmdlet in various scenarios.

    1. Listing All Files and Folders in a Directory

    This basic command lists all files and folders in the specified directory:

    Get-ChildItem -Path "C:\YourDirectory"

    2. Recursively Listing All Files in a Directory and Subdirectories

    Use the -Recurse parameter to include all subdirectories:

    Get-ChildItem -Path "C:\YourDirectory" -Recurse

    3. Finding Files with a Specific Extension

    This command lists all .txt files in the given directory:

    Get-ChildItem -Path "C:\YourDirectory" -Filter "*.txt"

    4. Listing Only Directories

    The -Directory parameter restricts the output to directories only:

    Get-ChildItem -Path "C:\YourDirectory" -Directory

    5. Listing Only Files

    Conversely, the -File parameter lists only files, excluding directories:

    Get-ChildItem -Path "C:\YourDirectory" -File

    6. Excluding Specific Items

    This example excludes all .log files from the listing:

    Get-ChildItem -Path "C:\YourDirectory" -Exclude "*.log"

    7. Listing Hidden Files

    To see hidden files in the directory, use the -Hidden parameter:

    Get-ChildItem -Path "C:\YourDirectory" -Hidden

    8. Finding Large Files in a Directory

    This command finds files larger than 50MB:

    Get-ChildItem -Path "C:\YourDirectory" -Recurse | Where-Object { $_.Length -gt 50MB }

    9. Listing Files Modified Before a Certain Date

    Find files last modified more than 30 days ago:

    Get-ChildItem -Path "C:\YourDirectory" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }

    10. Listing Files and Directories with Specific Attributes

    This example lists all read-only files and directories:

    Get-ChildItem -Path "C:\YourDirectory" -Attributes ReadOnly

    Each of these examples showcases a different aspect of the Get-ChildItem cmdlet, making it an indispensable tool for file system navigation and management in PowerShell.

    Conclusion

    In wrapping up our discussion on the Get-ChildItem cmdlet in PowerShell, it’s evident that this tool is a vital component for efficient file and system management. Throughout this guide, we’ve covered a range of functionalities, from basic file listing to advanced usage for specific scenarios like registry and certificate management.

    Key Points

    • Versatility: Get-ChildItem goes beyond simple file listing, offering extensive capabilities for various administrative tasks.
    • Customization: The cmdlet’s diverse parameters and compatibility with other PowerShell commands allow for highly specific and useful queries.
    • Cross-Platform Functionality: With PowerShell Core, Get-ChildItem is useful across different operating systems, though there are some platform-specific considerations.

    Coding and AutomationSystem AdministratorWindows

    Want to improve your IT skillset? Start with a free account and get access to our IT labs!

    Blogpostctadesktop

    Sign up free and start learning today!

    Practice on REAL servers, learn from our video lessons, interact with the Server Academy community!