PowerShell provides commands, to display the list of files & folders in the file system. Below are the cmdlets, commonly we use to get these details;
Display files and folders list
Get-Item
is one of the cmdlet used to get the item (for example: directory, file, registry entry etc.,) details at the specified location. By defaultm it gets only one item, unless you specify wildcard (*). When you specify wildcard (*) it gets the child items at the specified location.
We can use Get-Item
cmdlet, to get the details of the file or the folder. For example: if you want to see the details of the folder “C:\WINDOWS”, we can use this cmdlet, like below;
PS C:\> Get-Item "C:\WINDOWS" Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 8/14/2018 3:15 PM WINDOWS PS C:\>
Observe that, Get-Item
by default displays only one entry; not all the files in the folder. If you want to get the details of multiple file(s) or the folder(s); we can give the names in double quotes (“) – which is optional and separated by comma (,). For example: below command displays the details of the folder & the file at a time. Observe that, the folder ( Dot (.) – means, current directory) and the file name; are separated by comma (,).
PS C:\WINDOWS> Get-Item "." , "regedit.exe" Directory: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 8/14/2018 3:15 PM WINDOWS Directory: C:\WINDOWS Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 7/14/2009 7:09 AM 427008 regedit.exe PS C:\WINDOWS>
As mentioned above, Get-Item
can display the content of the Item if you use the wildcard (*). Does content mean file content.? No. For example: if the item is the Directory, if we use wildcard (*) with Get-Item
; it will display files which are in the Directory.
Below command will display files which are in the current directly. Observe that, PS prompt shows that the directory is C:\WINDOWS. That means, below command display files which are in the C:\WINDOWS directory. Also observe that, we used wildcard (*) to display multiple items;
PS C:\WINDOWS> Get-Item .\*
If we use below command, it displays the files which are having the file extension “.exe”.
PS C:\WINDOWS> Get-Item *.exe
Get-ChildItem
is another cmdlet, PowerShell provides to display the content of the Item. By default Get-ChildItem
will display all the items at the specified location; where Get-Item
will display only one item.
Below command will display the files which are in the current directory.
PS C:\WINDOWS> Get-ChildItem
Both these cmdlets, are used for the same purpose. But we have to pass more parameters to Get-Item
command to produce the same result; what we get through Get-ChildItem
command.
Display, only Files
We can restrict to display, only the files, by specifying “-File
” parameter to this command. Below command displays, only the Files from the current directory.
PS C:\WINDOWS> Get-ChildItem -File
Display, only Directories
We can also restrict to display, only the directories, by specifying “-Directory
” parameter. Below command displays, only the Directories from the current directory.
PS C:\WINDOWS> Get-ChildItem -Directory
But, when we use Get-Item
cmdlet; we need to write script or pass more parameters to get the same result.
If you like this Article, please give the feedback through below Comments section.
(Raju)
One thought on “PowerShell – Display files and folders list”