dir /a command is Get-ChildItem -Force. This cmdlet retrieves all items in a directory, including hidden and system files that are typically excluded from standard listings. By default, Get-ChildItem only shows visible items, but adding the -Force switch overrides this behavior to expose every file attribute stored on the filesystem.
Understanding how to list all files is crucial for system administration, debugging configuration issues, or performing deep cleanups of temporary data. While the Command Prompt uses flags like /a, /ah, or /as to filter by attribute type, PowerShell takes a different approach by exposing object properties rather than just text output. This allows for more robust scripting and filtering capabilities later in your workflow.
Basic Usage and Equivalency
In Windows CMD, runningdir /a lists all files regardless of their attributes. In PowerShell, you achieve the exact same result with:
Get-ChildItem -Force
This command works in any directory context. If you are currently in C:\Users\Public, it will list every single item there. The -Force parameter is the key differentiator here. Without it, PowerShell hides items marked as Hidden or System, which matches the default behavior of dir without switches.
You can also combine this with path specification:
Get-ChildItem C:\Windows -Force
This explicitly targets the Windows directory and forces the display of protected system files, such as bootmgr or registry hives, which are usually invisible to casual users.
Difference Between CMD Flags and PowerShell Switches
CMD offers granular control over attributes using specific flags. For example,dir /ah shows only hidden files, while dir /ad shows only directories. PowerShell does not have direct one-to-one flag equivalents for these filters because it treats files as objects with properties. Instead, you use pipeline filtering with Where-Object.
To replicate dir /ah (hidden files only):
Get-ChildItem -Force | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Hidden }
To replicate dir /ad (directories only):
Get-ChildItem -Force -Directory
Note that -Directory is a built-in switch in modern PowerShell versions that simplifies filtering for folders. It is much cleaner than using Where-Object checks for PSIsContainer.
For those interested in managing history or cleanup tasks after listing these files, see our guide on View PowerShell History Without PSReadline: A Comprehensive Guide which helps manage session data effectively.
Filtering by Specific Attributes
Sometimes you need to find files that are both hidden and read-only, or perhaps exclude system files entirely. PowerShell’s bitwise operators make this precise. To find all files that are NOT system files:Get-ChildItem -Force | Where-Object { -not ($_.Attributes -band [System.IO.FileAttributes]::System) }
This script block checks the Attributes property of each item. The -band operator performs a bitwise AND operation. If the result is non-zero, the attribute exists. Wrapping it in -not excludes those items.
You can also filter by name patterns simultaneously:
Get-ChildItem -Force -Filter "*.log" | Select-Object Name, Attributes, Length
This command finds all log files, forces visibility, and then selects only the relevant columns for easier reading. Using Select-Object transforms the output into a custom view, which is often more useful than the default table format.
Recursive Listing with Force
Thedir /a command in CMD does not recurse by default; you must add /s for subdirectories. In PowerShell, recursion is handled by the -Recurse switch. Combining it with -Force ensures you see everything in the entire tree structure.
Get-ChildItem -Path C:\Data -Recurse -Force
Be cautious when using -Recurse on large drives, as it can generate significant output and impact performance. You might want to limit the depth or filter aggressively.
If you plan to delete unwanted files found during such a recursive scan, refer to Unlock and Delete Files/Folders via Command Line (cmd/PowerShell) on Windows - Preferably Using FLOSS Tools for safe removal strategies.
Common Pitfalls and Permissions
Even with-Force, some system files may still not appear if your user account lacks sufficient permissions. Administrator privileges are often required to access certain protected directories like C:\Windows\System32\config. Always run PowerShell as Administrator when dealing with critical system files.
Another common issue is confusing Hidden file attributes with Archive attributes. Many backup tools set the Archive bit. If you want to see files marked for backup, ensure you understand that -Force reveals them, but they are not necessarily "hidden" in the UI sense.
Frequently Asked Questions
Why does Get-ChildItem hide files even with -Force?
If files remain hidden despite using -Force, you likely lack administrative permissions. Some system folders require elevated rights to enumerate contents. Run PowerShell as Administrator to resolve this access denial.
How do I list only hidden files without system files?
Use a pipeline filter combining the Hidden attribute check and excluding the System attribute. The command Get-ChildItem -Force | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Hidden -and -not ($_.Attributes -band [System.IO.FileAttributes]::System ) } achieves this precise selection.
Is there a shortcut for Get-ChildItem?
Yes, you can use the alias gci, ls, or dir. These aliases map directly to Get-ChildItem. Typing gci -Force works identically to the full cmdlet name and is faster to type in interactive sessions.
Can I export the list of all files to a CSV?
Absolutely. Pipe the output of Get-ChildItem -Force -Recurse into Export-Csv. For example: Get-ChildItem -Force -Recurse | Select-Object FullName, Attributes, LastWriteTime | Export-Csv -Path files.csv -NoTypeInformation creates a structured report of all items.
What is the difference between /a and /ah in CMD?
The /a flag lists all files regardless of attributes. The /ah flag lists only files with the Hidden attribute set. In PowerShell, -Force acts like /a, while filtering with Where-Object acting like /ah provides the selective visibility.