Introduction file listing using the PowerShell command line tool.
PowerShell is a powerful tool used for executing commands and scripting in Windows environments. You can for example list files using PowerShell for example. While copying files is an essential task, listing files, folders, and subfolders is equally important and can be done using PowerShell’s Get-ChildItem command. This tutorial provides a comprehensive guide on listing files using PowerShell.
Table of Contents
1. List files and folders using PowerShell (including subfolders)
Windows PowerShell provides the Get-ChildItem command, allowing you to list files and directories within a specified path. It can be combined with other options and parameters for more complex listing tasks.
Basic Listing of files in a folder
To list all files and directories in a specific folder:
Get-ChildItem C:\WORK\Folder

List files and Filter By Extension
For example, to list only the text files identified with the .txt
extension, use the wildcard and the txt in the filter, like in the example below:
Get-ChildItem C:\WORK\Folder -Filter "*.txt"
Excluding Specific Items
To exclude some files, use the same principle, as in the previous section, but this time with the Exclude keyword, like the .docx
Word documents like below:
Get-ChildItem C:\WORK\Folder -Exclude "*.docx"
2. Listing files and working with attributes
PowerShell is a powerful scripting language and shell used by administrators to automate tasks in Windows. Working with file attributes in PowerShell allows you to manage file properties like size, type, creation date, and permissions programmatically. Here’s how you can do it:
File Size
You can retrieve the file size using the Get-Item
cmdlet. Here’s an example to check the file size on the disk.
$file = Get-Item "C:\WORK\Folder\file.txt" $file.Length
This will return the file size in bytes. Learn more about working with file sizes in PowerShell.
File Type
You can identify the file type by checking its extension. Here’s how you can do that:
$file = Get-Item "C:\WORK\Folder\image.jpg" $file.Extension
This will return the extension, such as .jpg
. Explore more about file types and extensions in PowerShell.
Creation Date
You can retrieve the creation date of a file using the following code:
$file = Get-Item "C:\WORK\Folder\file.txt" $file.CreationTime
This will return the creation date and time. Discover more methods to find the creation date of files in PowerShell.
Permissions
You can manage file permissions using the Get-Acl
and Set-Acl
cmdlets. Here’s an example of how to retrieve permissions:
$acl = Get-Acl "C:\path\to\file.txt" $acl.Access
3. Combining with Other Cmdlets
Combine Get-ChildItem with other cmdlets for more advanced operations. For example, list files and pipe them to another command:
Get-ChildItem C:\WORK\Folder | ForEach-Object { /* Do something with each item */ }
Conclusion
Listing files using PowerShell provides a powerful way to manage and manipulate file structures within Windows. The Get-ChildItem command offers various options for filtering, excluding, and working with different attributes, providing flexibility for different tasks. To go further, let’s dive into how to copy files recursively using PowerShell.